Operators are fun. Using them is fun, and creating them is fun. We'll cover both in this post. It will be fairly short, but hopefully you'll find something interesting.
Note, I won't quite be covering Operator precedence. See S03 for details on that note.
Operators. Operators? Operators.
Junctions
Junctions are a nice semantic addition to Perl 6; in short, a junction is a single value that represents multiple values. You essentially operate on top of these in parallel, and they can be quite useful.
Junctions are important enough to where the purpose of the operators
|, & and ^ have been delegated to them, rather than being bit-twiddling operations as you'd think at first. With junctions, there are also the special functions any, one, all that are for their list counterparts:pugs> any(1..5)
(1 | 2 | 3 | 4 | 5)
pugs> one(1..5)
(1 ^ 2 ^ 3 ^ 4 ^ 5)
pugs> all(1..5)
(1 & 2 & 3 & 4 & 5)
pugs>As you can see from the example, the meaning of the operators should be somewhat evident (now, at least.) How can we use them?pugs> if 1 == any(1..10) {
....> say "works";
....> }
works
Bool::True
pugs> if 1 == all(1..10) {
....> say "works";
....> }
undef
pugs> if 1 == one(1..10) {
....> say "works";
....> }
works
Bool::True
pugs>(Note that I am utterly terrible normally at thinking up code examples, if you guys can do any better I'll take all I can get.)Naturally in the above example, your semantics and list you use will change, but you should get the idea that when you operate over junctions, you're operating over one aggregation of different things. An element in a junction is orthognoal to it's counterparts as order is not relevant.
Junctions are a useful addition. In general, you'll probably more directly deal with composing them from lists than directly per se, however, knowing the idea behind what every junctive operator does is good to know.
Ranges
The range operator,
.. is used to denote the set of values from one point to another. From 1 to 10, a to e, etc. etc... You can use them to represent values from positive infinity to negative infinity, a to z, 1 to whatever, ex:0..* # 0 to +infinity
*..0 # -infinity to 0
'a'..'e' # a to e
1..10 # one to twoHopefully, most of these semantics should be obvious even without the comments.Feed operators
Feeds are a lot like piping. You can take the return values from a function and 'feed' them to another quite seamlessly. For example, the results from map can be fed into your own custom function:
map { $_ % 2 == 0 },(1..10) ==> functorThe distinction between the two feed operators, <== and ==> are simply in which way your data flows.Symbolic Unary operation
In the case of many operators, when used in an unary form, impose a context on the operation in general. For example, where ~ was normally string contatenation, when used in an unary form, i.e.
~$x you are forcing a string context on your variable. This definition is somewhat strict; an unary context for an operator can do a lot of things.What're some of the things you can do with this? There is, for example, an 'from 0 to this' operator:
pugs> my $l = 10;
pugs> map { .say },^$l
0
1
2
3
4
5
6
7
8
9
pugs>You can also use this form to turn a list of numeric values into a list of string values:pugs> ~1..10
("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
pugs>Or, you can also use the equal sign to iterate over an iterator. For example:[altair@stormwind diveintoperl6]$ cat sayit.pl
say "enter something...";
my $l;
while $l = =$*IN {
say "you entered '"~$l~"'";
}
[altair@stormwind diveintoperl6]$ pugs sayit.pl
enter something...
hello
you entered 'hello'
hi dive into perl6 crowd!
you entered 'hi dive into perl6 crowd!'
[altair@stormwind diveintoperl6]$A list of all these rules can be found in S03.Meta operator[s|ing]
Hyper operators
Hyper operators are a way of taking an operator, applying it to each element of it's list, and returning the resultant list. In this sense, it's somewhat similar to
map.A hyper operator is denoted by the operator surrounded by a >> and a <<. There are variations to this however, which we'll shortly see. Here's just an example of taking two lists, adding each of their elements to the corresponding one, and returning it:
pugs> (1,2,3,4,5) >>+<< (5,4,3,2,1)
(6, 6, 6, 6, 6)
pugs>
What about an unary operator? Just use one of the symbols denoting a hyper operator, on the side which the arguments are expected:pugs> ~<< (1..10)
("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
pugs>But what if the two lists are not of the same length? How would you simply increase every element in a list by one? At first it may seem as though you need some sort of 'hackery,' but Perl 6 will sufficiently upgrade it, however, you must 'point' the hyper operator towards the smaller list:(1,2,3) >>+>> 1 # (2,3,4)If you simply 'point' your hyper operator towards the short side, perl will take care of the rest. If you don't know which side will be smaller than the other (if at all,) simply make the hyper operators 'point outwards':@a <<+>> @bReduction operatorsReduction operators are a meta operator that are used with a traditional infix operator, and they are used to 'reduce' a list of values into a single one. For example, here's a good one taken from freenode #perl6's channel title:
pugs> [~] <m oo se>
"moose"
pugs>If you're familiar with Haskell, this is the same:Prelude> foldr1 (++) ["m","oo","se"]
"moose"
Prelude>
Cross operatorsA cross operator is acheived by putting an infix operator inbetween two X's. It uses the infix operator and generates all permutations of the two lists given to it. Ex:
<austin brian> X~X <seipp stanford>
'austinseipp','austinstanford','brianseipp','brianstanford'It's roughly that simple (note, not yet implemented.)Creating operators
In Perl 6 you get a lot of the fun power of creating your own operators. What can they do? Well, just about whatever you want.
Operators in Perl 6 are typically defined as multi-subroutines, as you can have different types and contexts they're used in, i.e. you may need to add support for + to work with a class you define. Fairly typical stuff.
How do we define an operator? They are defined in the form of:
multi sub x:<y> (z) { ... }Where:- x is one of
infix,prefix,postfix,circumfixorpostcircumfix. Essentially, these are the categories your operator fits into. - y is your operator. The characters can be any non-whitespace characters, unicode included.
- z is/are your operands.
<+> or {'+'}, etc. etc..Unary operators can be defined either as
prefix or postfix, with one operand (naturally.) Binary operators are defined as infix, and bracketing operators (for example, in HTML, the two parts of a comment: <!-- and -->) are defined with circumfix. postcircumfix is used where a postfix is expected.This is best explained with a couple of good examples. For example, here's the reverse form of the
xx operator:pugs> multi sub infix:<~||~> (Int $n, Str $s) {
....> return $s xx $n;
....> };
pugs> 5 ~||~ "hi"
("hi", "hi", "hi", "hi", "hi")
pugs>What about a unary operator, that, say, uppercases the string that is prefixed to it?pugs> multi sub postfix: (Str $s) {
....> $s.uc;
....> };
pugs> "arg"!!!
"ARG"
pugs>How about a 'plus or minus' operator?pugs> multi sub prefix:<+/-> (Int $n) {
....> return +$n|-$n;
....> };
pugs> +/-5
(-5 | 5)
pugs>We could even define our own comment operator:multi sub circumfix:{'~#','#'} ($s) { return ""; }
~# how incredibly fun it is to conceive operators #
say "hello world";(Note, the [post]circumfix category for operators doesn't work yet (r16707), but hopefully it should soon.)Conclusion
This was a short post but a fun one. In previous languages, operators seemed quite boring and trivial, but hopefully Perl 6's new features will show you how to have fun with such a simple primitive. :)
28 comments:
Hi,
>A cross operator is acheived by putting an
>infix operator inbetween two X's. It uses the
>infix operator and generates all permutations
>of the two lists given to it.
>[austin brian] X~X [seipp stanford]
>'austinseipp','austinstanford','brianseipp',
>'brianstanford'
Those aren't actually permutations, but the cartesian product of the two sets. A permutation would be:
(1,2,3) ->
((1,2,3),(1,3,2),(2,1,3),(2,3,1),(3,1,2),(3,2,1))
Kind regards,
Elias Pipping
I don’t understand how a postcircumfix operator is used — if it’s circumfix, how can it be postfix as well? An example would help.
Great work!
But I believe i found an error in "Ranges":
1..10 # one to two
Shouldn't it be one to ten?
@Elias & Thomas: thanks for the fixes. :)
@anonymous: an example of a postcircumfix operator is adding the square-brackets onto the end of an array when you wish to get an element, i.e.
@a[0];
Your brackets are your postcircumfix operators in this instance. If I can find a sufficient example for them, I'll be certain to add them when I get the time.
look this is the "diet" i told you about you should really enter the site :) bye enter the site
女性会員様増加につき、当サイトの出張ホストが不足中です。女性の自宅やホテルに出向き、欲望を満たすお手伝いをしてくれる男性アルバイトをただいま募集していますので、興味のある方はTOPページから無料登録をお願いいたします
性欲のピークを迎えたセレブ熟女たちは、お金で男性を買うことが多いようです。当、熟女サークルでは全国各地からお金持ちのセレブたちが集まっています。女性から男性への報酬は、 最低15万円からとなっております。興味のある方は一度当サイト案内をご覧ください
Thank you very much, this was concise and well written. I enjoyed reading it, and I'm very glad you take the time to write and help out all of us who are just learning.
You're awesome!
:-)
セレブラブでは心とカラダに癒しを求めるセレブ女性と会って頂ける男性を募集しています。セレブ女性が集まる当サイトではリッチな彼女たちからの謝礼を保証、安心して男性はお金、女性は体の欲求を満たしていただけます。興味がある方は当サイトトップページからぜひどうぞ
ゲイの数が飛躍的に増えている現代、彼らの出逢いの場は雑誌やハッテン場からネットに移り変わってきています。当サイトは日本最大のゲイ男性の交流の場を目指して作られました。おかげさまで会員数も右肩上がりに伸びています。ゲイの方や興味のある方はぜひ当サイトをご覧ください。
さびしい女性や、欲求不満な素人女性たちを心も体も癒してあげるお仕事をご存じですか?女性宅やホテルに行って依頼主の女性とHしてあげるだけで高額の謝礼を手に入れる事が出来るのです。興味のある方は当サイトTOPページをご覧ください
パーティーや合コンでも使える右脳左脳チェッカー!あなたの頭脳を分析して直観的な右脳派か、理詰めな左脳派か診断出来ます。診断結果には思いがけない発見があるかも!みんなで診断して盛り上がろう
女性会員様増加につき、出張ホストのアルバイトが不足中です。ホテルや女性の自宅に出向き、彼女たちの欲望を満たすお手伝いをしてくれる男性アルバイトをただいま募集しております。興味のある方はTOPページをご覧ください
美容院いってきた記念に写メを更新しました。結構気に入ってるんですけどどうですか?メール乗せておくのでメッセお待ちしてるなりmiracle.memory@docomo.ne.jp
最近雑誌やTVで紹介されている家出掲示板では、全国各地のマンガ喫茶等を泊り歩いている家出少女のメッセージが多数書き込みされています。彼女たちはお金がないので掲示板で知り合った男性の家にでもすぐに遊びに行くようです。あなたも書き込みに返事を返してみませんか
プロフ作ったわいいけど見てくれる人いなくて少し残念な気分に陥ってます。意見でもいいので見た方がいましたら一言コメント送ってくだしゃいメアドのせているのでよろしくでしゅapotheosis@docomo.ne.jp
乱交パーティー実施サークル、「FREE SEX NET」では人に見られること、人に見せつける事が大好きな男女が集まり、乱交パーティーを楽しむサークルです。参加条件は「乱交が好きな18歳以上の健康な方」です。興味がある方はぜひ当サイトをご覧ください
家出している女の子と遊んでみませんか?彼女たちはお金に困っているので、掲示板で知り合ったいろんな男の家を泊り歩いている子も多いのです。そんな子たちとの出逢いの場を提供しています
出会ぃも今は逆¥交際!オンナがオトコを買う時代になりました。当サイトでは逆援希望の女性が男性を自由に選べるシステムを採用しています。経済的に成功を収めた女性ほど金銭面は豊かですが愛に飢えているのです。いますぐTOPページからどうぞ
友達の前では少し強がって彼氏なんかいらないって言ってしまうけど、やっぱ本音では欲しいです、夜は寒いし寂しいし私の本音に気付いてください。メアド乗せておくので優しい方連絡くださいtoward.the-future@docomo.ne.jp
セレブと言われる世の若妻は男に飢えています、特に地位が邪魔して出会いが意外と少ないから、SEXサークルを通じて日頃のストレス発散に毎日男を買い漁っています。ここは彼女達ご用達の口コミサイトです
一人で家出したんだけど助けてほしいです。今まで強がってました。もう親には頼れない…super-love.smile@docomo.ne.jp
激レア映像!芸能人のお宝映像を一挙公開中!無料登録するだけでレアなお宝映像やハプニング画像が取り放題、期間限定の動画も見逃せない
仕事を辞めてください。一日で今の月収を超えるお誘いがあります。某有名セレブ熟女の強い要望により少しの間、恋人契約という女性からのお申し込みがありました。今までは地位や名誉のために頑張ってこられたようでございますが年齢を重ね、寂しさが強くなってきたようでございます。男性との時間を欲しがっている女性に癒しを与えてくださいませ
ストーカーの追い回されて怖いんです。毎日夜になると非通知電話多いし怖い。。。助けてくださいpeach-.-girl@docomo.ne.jp
復活、スタービーチ!日本最大の友達探しサイトがついに復活、進化を遂げた新生スタービーチをやってみませんか?理想のパートナー探しの手助け、合コンパーティー等も随時開催しています。楽しかった頃のスタビを体験しよう
「友達の中で処女なのは私だけ…でも恥ずかしくて処女だなんて言えない、誰でもイイからバージンを貰ってほしい!」そんな女性が沢山いる事をご存じですか?出合いが無かった、家が厳格だった等の理由でHを経験したことがない女性がたくさんいるのです。当サイトはそんな女性たちと男性を引き合わせるサイトです
メアド開運、あなたの使ってるメアドを診断出来ちゃうサイト!吉と出るか凶と出るかはあなた次第、普段使ってるメアドの金運、恋愛運が測定できちゃいます
Post a Comment