Notations for fold, mapi, multi-yield etc.

62 views
Skip to first unread message

Jon Harrop

unread,
Mar 21, 2014, 8:34:29 AM3/21/14
to pragmatic-functional...@googlegroups.com
F# has a nice alternative notation for operations like map, collect, filter, choose, append and singleton in the form of sequence expressions:

    List.map f xs

    [ for x in xs -> f x ]

but there are no handy notations for fold, mapi, pick, mapFold, mapReduce nor expressions that produce more than one sequence (multi-yield). What notations exist for these in other programming languages, if any?

Cheers,
Jon.

Don Syme

unread,
Mar 21, 2014, 10:56:40 AM3/21/14
to pragmatic-functional...@googlegroups.com, jonathand...@googlemail.com
Some details of the F# notation are here: http://msdn.microsoft.com/en-us/library/dd233209.aspx

Michael Ciccotti

unread,
Mar 21, 2014, 11:53:15 AM3/21/14
to pragmatic-functional...@googlegroups.com, jonathand...@googlemail.com
In python, mapi is achieved by simply defining a function which creates a sequence consisting of tuples containing the item and the index.


    def enumerate(collection):
        'Generates an indexed series:  (0,coll[0]), (1,coll[1]) ...'    
        i = 0
        it = iter(collection)
        while 1:
            yield (i, it.next())
            i += 1

Used like this:
[i*j for i, j in enumerate(mylist)]

Anton Tayanovskyy

unread,
Mar 21, 2014, 12:58:52 PM3/21/14
to pragmatic-functional...@googlegroups.com, Jon Harrop
Racket has a bunch of forms like for/fold, for/sum, etc. They also
have mutli-valued sequences, but those are a bit of a pain to use from
Typed Racket.

http://docs.racket-lang.org/reference/for.html

Perhaps it's just habit, but I prefer how F# does it. Thankfully the
essentials of F#-style syntax are easy to define in Racket.

--A
> --
> You received this message because you are subscribed to the Google Groups
> "Pragmatic functional programming research" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
> pragmatic-functional-progr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Kind Regards,
Anton Tayanovskyy

WebSharper(tm) - type-safe JavaScript in F#
http://intellifactory.com
Reply all
Reply to author
Forward
0 new messages