These are chat archives for evhub/coconut
.5 |> (+)$(1).
(+)captures the + operator as a function. The
$means auto-curry'ing, and then call the function with one argument of value 1, yielding what you wrote as
(+)$(1)is tackling details of the implementation
+1looks nicer, but since Coconut is a strict superset of Python, that syntax is off-limits, since
+1is valid Python syntax for positive one. @yggdr is correct that the right way to do it would be
(+)$(1). As for libraries, you have all of Python at your disposal, since Coconut compiles to Python any Python library is usable from Coconut.
bindthen call it using Coconut's infix notation as
a `bind` b. You could use PyMonad from Coconut if you wanted, for example. Another alternative to
+1that you can use, by the way, would be
(-> _ + 1), using implicit lambda syntax, if you think that looks nicer.
a >> bwhere a is a MyClass and b a function?
None" or "oh, is this list empty?" etc... where stuff like Functors (as in
fmap) would allow me to easily just chain together something
mapis sufficient for most purposes like that--can you give me a specific example?
map(func, iterable)returns an iterator where
funcis applied to every element of
fmapsays "return something of the same type with this function mapped over the contents", while
mapsays "regardless of what type you give me, return an iterator over the contents of the thing with this function mapped over it". If you want to get back to the
fmapbehaviour, you can just call
liston the resulting iterator, although then you lose the performance benefits of keeping everything lazy until absolutely necessary (which, of course, you get automatically in a purely functional language like Haskell, but you don't in Python). As for
reduceis exactly the same thing.
data) behaves like a collection. This is all very abstract--if you give me an example of a problem you're trying to solve or some functional code you'd like me to translate, I'd love to show you how it all fits together.
data Nothing() data Just(n) Maybe = (Nothing, Just) fmap((*2), Just(3)) # should yield (Just(6)) fmap((*2), Nothing) # should yield (Nothing)
>>> data Nothing() >>> data Just(n) >>> Tree = (Nothing, Just) >>> Just(3) |> map$(-> _*2) |*> Just Just(n=6) >>> Nothing() |> map$(-> _*2) |*> Nothing Nothing()
fmapis exactly as I was saying--
fmappreserves the type of the object, whereas
mapdoes not. Honestly, if you wanted a function that behaves like
fmap, I could add it to Coconut pretty easily.
fmapis an endofunctor and
def fmap(func, obj) = obj |> map$(func) |*> obj.__class__
fmapto Coconut's built-ins. I'm not sure what I'm going to do about it yet, but it deserves an issue, for sure.
mapis structure-preserving for lists, but not for ADTs?
mapis never structure-preserving. It always returns an object of type
map, which supports all the basic iteration primitives (indexing, looping, reversing, etc.), but nothing else.