Monads/List monad: Difference between revisions

→‎{{header|Perl 6}}: Add Perl 6 example
(Added Kotlin)
(→‎{{header|Perl 6}}: Add Perl 6 example)
Line 336:
[DDDD, FFFFFF, HHHHHHHH]
</pre>
 
=={{header|Perl 6}}==
Perl&nbsp;6 does not have Monad types built in but they can be emulated/implemented without a great deal of difficulty. List Monads especially are of questionable utility in Perl&nbsp;6. Most item types and Listy types have a Cool role in Perl&nbsp;6. (Cool being a play on the slang term "cool" as in: "That's cool with me." (That's ok with me). So Ints are pretty much treated like one item lists for operators that expect lists. ("I want a list." "Here's an Int." "Ok, that's cool.") Explicitly wrapping an Int into a List is worse than useless. It won't do anything Perl&nbsp;6 can't do natively, and will likely '''remove''' some functionality that it would normally have. That being said, just because it is a bad idea (in Perl&nbsp;6) doesn't mean it can't be done.
 
In Perl&nbsp;6, bind is essentially map. I'll shadow map here but again, it '''removes''' capability, not adds it. Perl&nbsp;6 also provided "hyper" operators which will descend into data structures and apply an operator / function to each member of that data structure.
 
Here's a simple, if contrived example. take the numbers from 0 to 9, add 3 to each, find the divisors of those sums and print the list of divisors for each sum... in base 2. Again, a bind function was implemented but it is more limited than if we just used map directly.
 
The * in the bind blocks are typically referred to as "whatever"; whatever + 3 etc. The guillemot (») is the hyper operator; descend into the data structure and apply the following operator/function to each member.
<lang perl6>sub bind (@list, &code) { @list.map: &code };
 
sub divisors (Int $int) { gather for 1 .. $int { .take if $int %% $_ } }
 
put join "\n", (flat ^10).&bind(* + 3).&bind(*.&divisors)».base(2);</lang>
 
{{out}}
<pre>1 11
1 10 100
1 101
1 10 11 110
1 111
1 10 100 1000
1 11 1001
1 10 101 1010
1 1011
1 10 11 100 110 1100</pre>
 
=={{header|Ring}}==
10,327

edits