Thursday, July 5, 2007

Operating with operators

Pugs revision: r16707

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 two
Hopefully, 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) ==> functor
The 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 <<+>> @b
Reduction operators
Reduction 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 operators
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. 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, circumfix or postcircumfix. 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.
Actually, the part following the semicolon in the subroutine name (that's all operators are, special subroutines) can essentially be any form of a hash subscript, i.e. you could do <+> 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. :)

60 comments:

Anonymous said...

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

Anonymous said...

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.

Thomas Wittek said...

Great work!

But I believe i found an error in "Ranges":
1..10 # one to two

Shouldn't it be one to ten?

austin said...

@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.

barb michelen said...

look this is the "diet" i told you about you should really enter the site :) bye enter the site

出張ホスト said...

女性会員様増加につき、当サイトの出張ホストが不足中です。女性の自宅やホテルに出向き、欲望を満たすお手伝いをしてくれる男性アルバイトをただいま募集していますので、興味のある方はTOPページから無料登録をお願いいたします

熟女サークル said...

性欲のピークを迎えたセレブ熟女たちは、お金で男性を買うことが多いようです。当、熟女サークルでは全国各地からお金持ちのセレブたちが集まっています。女性から男性への報酬は、 最低15万円からとなっております。興味のある方は一度当サイト案内をご覧ください

Anonymous said...

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!

:-)

セレブ said...

セレブラブでは心とカラダに癒しを求めるセレブ女性と会って頂ける男性を募集しています。セレブ女性が集まる当サイトではリッチな彼女たちからの謝礼を保証、安心して男性はお金、女性は体の欲求を満たしていただけます。興味がある方は当サイトトップページからぜひどうぞ

ゲイ said...

ゲイの数が飛躍的に増えている現代、彼らの出逢いの場は雑誌やハッテン場からネットに移り変わってきています。当サイトは日本最大のゲイ男性の交流の場を目指して作られました。おかげさまで会員数も右肩上がりに伸びています。ゲイの方や興味のある方はぜひ当サイトをご覧ください。

素人 said...

さびしい女性や、欲求不満な素人女性たちを心も体も癒してあげるお仕事をご存じですか?女性宅やホテルに行って依頼主の女性とHしてあげるだけで高額の謝礼を手に入れる事が出来るのです。興味のある方は当サイトTOPページをご覧ください

右脳左脳チェッカー said...

パーティーや合コンでも使える右脳左脳チェッカー!あなたの頭脳を分析して直観的な右脳派か、理詰めな左脳派か診断出来ます。診断結果には思いがけない発見があるかも!みんなで診断して盛り上がろう

出張ホスト said...

女性会員様増加につき、出張ホストのアルバイトが不足中です。ホテルや女性の自宅に出向き、彼女たちの欲望を満たすお手伝いをしてくれる男性アルバイトをただいま募集しております。興味のある方はTOPページをご覧ください

友達募集 said...

美容院いってきた記念に写メを更新しました。結構気に入ってるんですけどどうですか?メール乗せておくのでメッセお待ちしてるなりmiracle.memory@docomo.ne.jp

家出掲示板 said...

最近雑誌やTVで紹介されている家出掲示板では、全国各地のマンガ喫茶等を泊り歩いている家出少女のメッセージが多数書き込みされています。彼女たちはお金がないので掲示板で知り合った男性の家にでもすぐに遊びに行くようです。あなたも書き込みに返事を返してみませんか

プロフ said...

プロフ作ったわいいけど見てくれる人いなくて少し残念な気分に陥ってます。意見でもいいので見た方がいましたら一言コメント送ってくだしゃいメアドのせているのでよろしくでしゅapotheosis@docomo.ne.jp

乱交パーティー said...

乱交パーティー実施サークル、「FREE SEX NET」では人に見られること、人に見せつける事が大好きな男女が集まり、乱交パーティーを楽しむサークルです。参加条件は「乱交が好きな18歳以上の健康な方」です。興味がある方はぜひ当サイトをご覧ください

家出 said...

家出している女の子と遊んでみませんか?彼女たちはお金に困っているので、掲示板で知り合ったいろんな男の家を泊り歩いている子も多いのです。そんな子たちとの出逢いの場を提供しています

逆¥交際 said...

出会ぃも今は逆¥交際!オンナがオトコを買う時代になりました。当サイトでは逆援希望の女性が男性を自由に選べるシステムを採用しています。経済的に成功を収めた女性ほど金銭面は豊かですが愛に飢えているのです。いますぐTOPページからどうぞ

彼氏募集 said...

友達の前では少し強がって彼氏なんかいらないって言ってしまうけど、やっぱ本音では欲しいです、夜は寒いし寂しいし私の本音に気付いてください。メアド乗せておくので優しい方連絡くださいtoward.the-future@docomo.ne.jp

若妻 said...

セレブと言われる世の若妻は男に飢えています、特に地位が邪魔して出会いが意外と少ないから、SEXサークルを通じて日頃のストレス発散に毎日男を買い漁っています。ここは彼女達ご用達の口コミサイトです

家出 said...

一人で家出したんだけど助けてほしいです。今まで強がってました。もう親には頼れない…super-love.smile@docomo.ne.jp

お宝映像 said...

激レア映像!芸能人のお宝映像を一挙公開中!無料登録するだけでレアなお宝映像やハプニング画像が取り放題、期間限定の動画も見逃せない

熟女 said...

仕事を辞めてください。一日で今の月収を超えるお誘いがあります。某有名セレブ熟女の強い要望により少しの間、恋人契約という女性からのお申し込みがありました。今までは地位や名誉のために頑張ってこられたようでございますが年齢を重ね、寂しさが強くなってきたようでございます。男性との時間を欲しがっている女性に癒しを与えてくださいませ

メール said...

ストーカーの追い回されて怖いんです。毎日夜になると非通知電話多いし怖い。。。助けてくださいpeach-.-girl@docomo.ne.jp

スタビ said...

復活、スタービーチ!日本最大の友達探しサイトがついに復活、進化を遂げた新生スタービーチをやってみませんか?理想のパートナー探しの手助け、合コンパーティー等も随時開催しています。楽しかった頃のスタビを体験しよう

処女 said...

「友達の中で処女なのは私だけ…でも恥ずかしくて処女だなんて言えない、誰でもイイからバージンを貰ってほしい!」そんな女性が沢山いる事をご存じですか?出合いが無かった、家が厳格だった等の理由でHを経験したことがない女性がたくさんいるのです。当サイトはそんな女性たちと男性を引き合わせるサイトです

メアド開運 said...

メアド開運、あなたの使ってるメアドを診断出来ちゃうサイト!吉と出るか凶と出るかはあなた次第、普段使ってるメアドの金運、恋愛運が測定できちゃいます

逆¥交際 said...

今話題の逆¥交際!あなたはもう体験しましたか?当サイトでは逆援希望の女性が男性を自由に選べるシステムを採用しています。成功を収めた女性ほど金銭面は豊かですが愛に飢えているのです。いますぐTOPページからどうぞ

豪華賞品 said...

只今、シャープ32型液晶テレビ、PS3等、豪華商品が当たるキャンペーンを実施中!まずは欲しい商品を選び、メールアドレスを登録して無料エントリー!その場で当たりが出たら賞品ゲットできます。抽選に外れた方もWチャンスで商品券等が当たります。ぜひチャレンジして下さい

メル友 said...

彼氏にDVされて、ちょっと男性不信です。でも、恋愛して彼氏も作りたいと思ってるので、優しく接してくれる人を探してます。連絡待ってまぁす! pretty-toy-poodle@docomo.ne.jp

神待ち said...

家出をして不安な少女たちの書込が神待ち掲示板に増えています。一日遊んであげたり、家に招いて泊まらせてあげるだけで、彼女たちはあなたに精一杯のお礼をしてくれるはずです

乱交 said...

全国乱交連盟主催、スワップパーティーに参加しませんか?初めての方でも安心してお楽しみいただけるパーティーです。参加費は無料、開催地も全国に120ヶ所ありますので気軽に参加してお楽しみ下さい

アブノーマル said...

SM・露出・スワッピング・レズ・女装・フェチなど…普通じゃ物足りないあなたが思う存分楽しめる世界!貴方だけのパートナーを探してみませんか?アブノーマルでしか味わえない至福の時をお過ごしください

Anonymous said...

It is very interesting for me to read the post. Thanx for it. I like such themes and everything that is connected to them. I definitely want to read more soon.

セレブラブ said...

セレブラブでは毎月10万円を最低ラインとする謝礼を得て、セレブ女性に快楽を与える仕事があります。無料登録した後はメールアプローチを待つだけでもOK、あなたも当サイトで欲求を満たしあう関係を作ってみませんか

裏バイト said...

簡単に大金を稼ぐことができる裏バイトがあります。女性が好きな方、健康な方なら日給3万円以上の収入を得ることも可能です。興味のある方はHPをご覧ください

救援部 said...

一人Hを男性に見てもらうことで興奮する女性が多数いることをご存じですか?当サイト、救援部ではそんな女性たちが多数登録されています。男性会員様は彼女たちのオ○ニーを見てあげるだけで謝礼を貰えるシステムとなっております。

メル友募集 said...

冬に1人ボッチで家でご飯とかオヤスミなんて寂しすぎるょ~~っ!こんなところに書き込んだら削除されちゃいそうだけど少しでもきっかけ作らなくっちゃと思って書いてみましたっ!!気軽に会ったり出来たりする方ってこの掲示板見てませんか~!?良かったらメールくださいね★フリメだったら私気付けないんで携帯のアドレス乗せておくねっ!! love-sexy@docomo.ne.jp

失恋 said...

失恋は心に深い傷を残します。その傷を癒す特効薬、それは新しい出会い。あなたの心を癒す、素晴らしい出会いを当サイトで見つけて、笑顔を取り戻してください

高額バイト said...

高額バイトのご案内です。欲求不満になっている女性を癒して差し上げるお仕事です。参加にあたり用紙、学歴等は一切問いません。高収入を稼ぎたい、女性に興味のある方はぜひどうぞ

交際 said...

スタービーチの突然の閉鎖、優良な友達探しサイトが無くなってしまいましたが、新生・スタービーチがここに復活しました!、進化を遂げた新生スタービーチをやってみませんか?理想の交際探しの手助け、合コンパーティー等も随時開催しています。楽しかった頃のスタビを体験しよう

神待ちサイト said...

「家出してるんで、泊まるところないですか?」家出救済神待ちサイトには毎日このような女の子からの書き込みがされています。彼女たちはホテルや家に泊まらせてあげたり、遊んであげるだけであなたに精一杯のお礼をしてくれるはずです

露出 said...

普通のプレイじゃ絶対味わえない快感、それは野外露出プレイ。最初は嫌がっていた女も次第にハマっていって、その内それが快感に変わってきます。野外露出プレイで興奮度アップ間違い無し

友達 said...

早い時期に結婚してしまって、少し後悔しているんです。私は、今の夫しか経験が無くって、このままでいいのかなって…。思うようになってきてしまって…冒険はしたいんですけど、やっぱりばれたりしたら怖いので…割り切りで会える方って居ませんでしょうか?連絡お待ちしてますね♪最初に年齢を教えてくれるとうれしいです。pop-music-lo-ve@docomo.ne.jp

ライブチャット said...

当サイトでは無料でオナ動画を見ることができます。また、ライブチャット機能でリアルタイムオ○ニーを見るチャンスも高く、興奮間違いなしです。また、一人Hのお手伝いを希望される女性もあり、お手伝いいただけた方には謝礼をお支払いしております

スタービーチ said...

一時代を築いたスタービーチは閉鎖になりましたが、もう一度楽しい思いをしたい、もう一度出会いたいと思う有志により再度復活しました。本家以上に簡単に出会えて楽しい思いを約束します

女に生れて来たからには!! said...

誰にも言えない秘密があります。実はとってもHなんです、せっかく女として生まれたからにはアブノーマルな世界に飛び込んでみたいです☆普段では考えられないプレイを思う存分楽しみ、経験したいんです♪快楽に溺れさせてくれませんか?一緒に感じ合いましょう!!都合はつくのですぐに時間を合わせられます。18歳よりも上の方がいいです!! quietness@docomo.ne.jp

グリー said...

日本最大級、だれもが知っている出会い系スタービーチがついに復活、グリーより面白い新生スタビをやってみませんか?趣味の合う理想のパートナー探し、合コンパーティーに興味がある方はぜひ無料登録してみてください。楽しかった頃のスタビで遊んでみよう

SM度チェッカー said...

あなたのSM度を簡単診断、SM度チェッカーで隠された性癖をチェック!真面目なあの娘も夜はドS女王様、ツンデレなあの子も実はイジめて欲しい願望があるかも!?コンパや飲み会で盛り上がること間違いなしのおもしろツールでみんなと盛り上がろう

リッチセックス said...

女性には風俗がない!そんな悩みを持つセレブ女たちは、リッチセックスでお金を使い自分を満たします。金銭面では豊かですが、愛に飢えている彼女たちを癒して高額な報酬を手に入れてみませんか

友達募集中 said...

初書き込みで申し訳ないんですが、都合のいい男性探しています。不況の中でも会社が高成長してて、忙しい毎日です。お陰でプライベートが充実していなくって、溜まる一方です。財産的にも多少余裕が今のところあるのでお礼もできます。何より、この書き込みが読まれているのかちょっと怪しいですけど…。アドレス置いとくので、消されないうちにメールくれたら嬉しいです。inspiration.you@docomo.ne.jp

玉の輿度チェッカー said...

当サイトは、みんなが玉の輿に乗れるかどうか診断できる性格診断のサイトです。ホントのあなたをズバリ分析しちゃいます!玉の輿度チェッカーの診断結果には、期待以上の意外な結果があるかも

リア友 said...

メル友らんどでは誰でも気軽にメル友が作れちゃう、参加無料でいつでも利用可能なコミュニティサイト♪ご近所の気の合うリア友や、真面目に彼氏彼女探しなど、楽しみ方は無限大!自分にぴったりの相手を見つけちゃおう

お家遊びに来てくれる人いないかなぁ? said...

一人暮らし寂しいよ~(泣)誰かお家遊びにきてくれないかなぁ?休みの日とかも全然予定ありません。料理作るの得意だから来てくれたら食べてほしいな♪見た目は悪くないと思うから安心してください!(笑)細かい事は気にしないけど18歳未満の人は微妙かな、気軽に仲良くしてください milky-yukinko@docomo.ne.jp

神待ち said...

冬に1人でネカフェとか寂しすぎやし、でも自分から積極的に声を掛けれる娘ばかりと違い、内気な娘は神待ちと言われるように自分の事を助けてくれるのを待ってるんです。貴方の優しさを待ってる娘は意外な程多いよ

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

家出掲示板 said...

あなたのご近所の女の子たちと無料でカンタンにであえる家出掲示板!大学生・専門学生、まさかの女子○生まで!ちょっとしたお小遣い稼ぎに全国の女の子たちが殺到中!ノーピンクからちょっぴりHなお誘いまで…自分に合ったコを選んでメッセしちゃおう

名言チェッカー said...

簡単な設問に答えるだけで貴方にふさわしい名言がわかる、名言チェッカー!あなたの本当の性格を見抜いちゃいます。世界の偉人達が残した名言にはどことなく重みがあるものです

モバゲー said...

最高の遊び場、スタービーチ!日本最大の友達探しサイトがついに復活、モバゲーより面白い新生スタビをやってみませんか?理想のパートナー探しの手助け、合コンパーティー等も随時開催しています。楽しかった頃のスタビを体験しよう