7 ms·
From 3 minutes playing, junctions are cool... #!/usr/bin/env perl6 if 3|4|5 == 5|6 { print "Junction found\n" } else { print "No j
by harryf 11y ago
From 3 minutes playing, junctions are cool...
#!/usr/bin/env perl6
if 3|4|5 == 5|6 {
print "Junction found\n"
} else {
print "No junction\n"
}
...or using arrays this seems to be the way to do it...
#!/usr/bin/env perl6
my @first = <3 4 5>;
my @second = <5 6>;
if any(@first) == any(@second) {
print "Junction found\n"
} else {
print "No junction\n"
}
- hbex5 11y agoThat both indicates to me why Perl 6 is cool, and also why it's a bit mad. Junctions, in the core language? Really? Wouldn't they be best placed just being a library?
- awwaiid 11y agoYes! "Cool and also a bit mad" is a great description of Perl 6 (and maybe a few other awesome languages. At least until people get used to them... I guess we're all a bit mad!)
- deleted 11y ago[deleted]
- kbenson 11y agoAh, but when it's part of the core, that enables some other cool features (such as automatic spooling of operations to multiple CPUs for where applicable), and well thought out and stable interaction with other core features.
- Ovid 11y agoJunctions are insanely useful once you get used to them and I use them frequently. Basically, any time you have a list and you're worried the properties of any, all, or none of the list members (hmm, just about all lists), then junctions are often a very clean way of expressing the issue. if all(@ages) >= 18 { # let 'em in the club } Or: if any(@ages) < 18 { # don't let 'em in the club } Or: if none(@ages) < 18 { # don't let 'em in the club } So I use logic like the above quite a bit, however, there are plenty of areas where the above can be better if it's deeply integrated into the language. In this case, as already mentioned, it's auto-threading where appropriate. That's not an easy feature to get right if it's not baked into the language, but it's a huge win if done right. Perl 6 also has gradual typing baked in. Other languages have gradual typing as a library, but that often fails dramatically (http://blog.circleci.com/why-were-no-longer-using-core-typed/ http://blog.circleci.com/why-were-no-longer-using-core-typed...). In Perl 6, because it's built-in, not only are the failures described something Perl 6 can avoid, but there are also plenty of type checks Perl 6 can avoid at run time with integrated gradual typing (imagine that benefit in hot loops). You could even combine that with native junction auto-threading and presumably have super-fast comparisons where the needs for checking types is eliminated at run time because it's already been done at compile time. If you do that with libraries, it's awfully tricky to get right, if not impossible.
- masklinn 11y agoThey seem like a complexification of sets and set operations?
- viraptor 11y agoIs this just standard sets, like python's: In [1]: {3,4,5} & {5,6} Out[1]: {5} or is there something I'm missing?
- hahainternet 11y agoIt's not standard sets, it's a bit more than that, Perl 6 has sets of course, but Junctions should be able to autothread and express a sort of superposition of values. http://doc.perl6.org/type/Junction http://doc.perl6.org/type/Junction I could copy and paste the definition but it wouldn't be too helpful without context.
- arethuza 11y agoSo like software SIMD? When it says autothread does it mean actual separate threads/CPUs?
- hahainternet 11y agoIt's not so much SIMD. Perl 6 has a number of different constructs in which multiple operations can be carried out like that. However, yes the intention is to enable a large number of these scenarios to be carried out on multiple CPUs. I don't think Rakudo implements that yet, but there's quite a lot to improve first.
- awwaiid 11y agoThe | junctions seem a lot like sets to me, though I guess there is a lot of fancy you can use. One that comes to mind is that you can pretend that it is a single value, even if it is a complex junction of & and | things: > (5|4) + 1 any(6, 5) > (5&4) + 1 all(6, 5) > my $x = 5|4|3|2 any(5, 4, 3, 2) > say "yep!" if $x + 2 ~~ 7 yep! > ((2|3|4|5|6|7) & (3|4)) + 1 ~~ 5 True > ((2|3|4|5|6|7) & (3|4)) + 1 ~~ 6 False Kinda weird, especially & junctions. The docs[1] say "Junctions are meant to be used as matchers in boolean context; introspection of junctions is not supported. If you feel the urge to introspect a junction, use a Set or a related type instead." So far in my Perl 6 exploration I don't think I've used/needed junctions -- strikes me as a specific sort of use-case, but maybe familiarity would help me spot more (useful) opportunities. [1] http://doc.perl6.org/type/Junction http://doc.perl6.org/type/Junction, though like all P6 docs needs more contributions
- bhaak 11y agoI'm not sure. Maybe it does look fancy compared to perl 5 but is it really that compared to modern languages? If I look at the examples on this page https://en.wikibooks.org/wiki/Perl_6_Programming/Junctions https://en.wikibooks.org/wiki/Perl_6_Programming/Junctions it doesn't seem to be. For example "if ($x == 2 || $x == 4 || $x == 5 || $x == "hello" || $x == 42 || $x == 3.14)" can be done in Perl6 as "if ($x == (2|4|5|"hello"|42|3.14))" but in Ruby this would equally easy been done with "if [2,4,5,"hello",42,3.14].include?(x)". Ruby also has set operations defined on its container classes (e.g. [1,2,3]&[2,3,4] => [2,3]) and any?, all?, none? methods that take blocks. Perl6 looks like a language that might have been great if it had been released in the 90s but nowadays I wonder if it will gain enough traction from non perl programmers.
- hahainternet 11y ago> but in Ruby this would equally easy been done with "if [2,4,5,"hello",42,3.14].include?(x)". But that isn't the same is it. That's only a related feature because Ruby doesn't have first class Junctions. > I wonder if it will gain enough traction from non perl programmers. These days people have been taught to be arrogant. To assume their language is the best. This is what makes things a tough challenge.
- bhaak 11y ago>> but in Ruby this would equally easy been done with "if [2,4,5,"hello",42,3.14].include?(x)". > But that isn't the same is it. That's only a related feature because Ruby doesn't have first class Junctions. At least in this case, the only difference I see is that this is a language feature in Perl6 and a method from the standard lib in Ruby. And it has to be a language feature in Perl 6 as there you don't have open classes as in Ruby or an equivalent feature for overriding operators or adding functionality to standard lib types.
- hahainternet 11y ago> And it has to be a language feature in Perl 6 as there you don't have open classes as in Ruby or an equivalent feature for overriding operators or adding functionality to standard lib types. I'm not sure what makes you think this. I'm not very familiar with Ruby's internals but Perl 6's MOP certainly supports modifying classes, you can add your own operators etc. Could you provide a more explicit example?
- grondilu 11y agoI'm not sure this is how you're supposed to use junctions. I vaguely recall that the semantics is not obvious when you put a junction on both sides of a relational operator. It's much clearer when there is a junction on only one side. More typical uses are: die "unauthorized value" if $_ == 1|2; grep none(1, 2), @values; say "$a and $b are even" if ($a & $b) %% 2; Basically : junctions are not sets. Don't try to use them for set operations. Perl 6 has a Set structure. Use that instead.
- masklinn 11y agoThe first two seem to be set operations ("value in set" and "filter values not in set"), I've no idea what the third one is supposed to do, something like all ((==0).(`rem` 2))?
- bhaak 11y agoThe third seems to be shorthand for map and reduce (or only reduce). In Ruby, you could express the same with `[2,4,6].reduce(true) {|even,num| even & num.even? }` or more clearer IMO `[2,4,6].map(&:even?).reduce(&:&)`.
- grondilu 11y agoBy set operations I was more specifically thinking of intersection and union. Testing if an element belongs to a set can indeed be done with junctions but that's pretty much it. The third example shows the so-called auto-threading behavior. I think it's significantly different than what you can do with sets. A Junction in Perl 6 is "a superposition of data values pretending to be a single value". So for instance any algebraic expression can be used with a junction as if it was a scalar. The result will be a junction. In this example the expression is $_ %% 2, which is Perl 6's way of writing the boolean statement "the variable $_ is divisible by 2". We use this expression with ($a & $b) instead of $_.
- kqr 11y agoSo this is where I saw this. I distinctly remember seeing this feature in Perl many years ago, but then when I tried to find it I really, really couldn't, so I assumed it was something I had dreamt up. However ridiculous it sounds, a weight is lifted from my chest.
- mst 11y agoThere's also http://p3rl.org/Syntax::Feature::Junction http://p3rl.org/Syntax::Feature::Junction for perl5, which is a descendant of an early backport of the perl6 concept by Damian.
- josteink 11y agoSo basically == is overloaded to also be an operator which means "intersects". I mean, it's nice that this sort of functionality has nice and concise operators, but I'm coming from a school of thought where overloading the equality operator should not be done lightly :)
- masklinn 11y ago> So basically == is overloaded to also be an operator which means "intersects". Junctions seem way more complex than that, first there are 4 different junction contains (any, all, one and none), second they overload 12 comparison operators each and third the other operand doesn't have to be a junction. So e.g. any(@ar1) == any(@ar2) is an intersection but any(@ar1) == $val is a containment, and I've no idea what all(@ar1) < any(@ar2) would be, I guess it would check that max(ar1) < max(ar2)?
- senekerim 11y agoThis looks awful to my (mathematically trained) eyes. What is the rationale behind introducing this language feature?
- masklinn 11y agoI really couldn't tell you. I guess "it's perl, we can"?
- hahainternet 11y agoCould you explain why you think this is awful? Perl 6 has proper sets, bags and the appropriate operators for 'normal' set operations too.
- raiph 11y agoOne that I recall that I'm hoping still applies is having a syntactically and conceptually lightweight way to express semantically parallel computation logic. Given the code: junction1 = val1 | val2 | val3; junction2 = val4 & val5; if junction1 op junction2 { ... }; the compiler is free to fire off several threads, or set up a lower level SIMD parallel process, with short-circuiting, to compute the conditional result.