9 ms·
Functional Programming Jargon
- namanyayg 10y agoRequesting @dang to change the URL to remove the #list fragment.
- dang 10y agoWe did. But please send such questions to hn@ycombinator.com, because (a) they're off-topic and (b) it's the only way to make sure we see it.
- sdegutis 10y agoWhat's most interesting to me here is that this article uses JavaScript for all its examples. Also, this may well be the thing that finally helps me understand what the crap Monads are.
- jstimpfle 10y agoIt's not a bad example but lacking generality and more importantly explicit types. It's really all about connecting types like LEGO blocks, so the important thing is to show the type signatures. "bind" is the function required from Haskell's Monad typeclass, but not a good choice to explain monads. It's much cleaner to explain monads as functors with "join" and ("inject" aka return). map :: (a -> b) -> (M a -> M b) join :: M (M a) -> M a inject :: a -> M a With these guys you can easily compose "dirty" asymmetric operations like (readFile :: FilePath -> IO Bytes)
- jethrolarson 10y agoIt's hard to capture monad in a paragraph and a couple examples. The utility of chain/flatmap doesn't get too interesting until you apply it to the Option Type or Promises but I didn't want to require readers to come with knowledge of them. Maybe a promise-based example isn't asking too much though
- greydius 10y agoI took a (mandatory for cs) class in college that introduced these concepts. For a long time I thought that was a normal part of cs curriculums, but it seems not. Also, its very strange to see these concepts illustrated with Javascript. I imagine thats something like trying to learn Chinese using the roman alphabet. Not that it can't be done, but a lot of important details are necessarily missing.
- bjz_ 10y ago> For a long time I thought that was a normal part of cs curriculums, but it seems not. Alas, not all CS programs are created equal. Many skip entirely over FP because it isn't 'what the industry does'.
- GFK_of_xmaspast 10y agoPinyin is a thing that exists tho?
- whorleater 10y agoPinyin's existence is as a stepping stone, and misses out on a lot of interplay between words. Pinyin negates how you can separate Chinese characters into different symbols, such as the Chinese character '鮮' ('xiān' in pinyin), is the combination of '魚' ('yú') and '羊' ('yáng'). It's certainly not impossible to learn Chinese through pinyin, but it would be an odd choice.
- 208Gi 10y agoPinyin doesn't negate Chinese characters. It's just tackling another issue - sounds and tones.
- DeadReckoning 10y agoIt's not an odd choice. I've never heard of anyone learning Chinese as a second language who doesn't use pinyin at least for the first few years.
- GFK_of_xmaspast 10y ago
- white-flame 10y agoDefining what things are doesn't describe what they're useful for. Consider the Monad entry; does this bring any clarity to a beginning functional programmer who's looking up why people use monads?
- psibi 10y agoAlso I don't think the definition of Monad itself is accurate there.
- asQuirreL 10y agoAs far as I can tell, the definition is accurate. The only thing I would say is that `flatMap` is the name given to the (usual) list monad's bind operation, and is not used for arbitrary monad's binds.
- eru 10y agoTheir definition of 'Lift' is interesting. In essence with that definition they could redefine Applicative Functors as something that's a 'Lift' and a Pointed Functor.
- TheMagicHorsey 10y agoI wish the utility of these concepts was illustrated with some code. Although, I concede perhaps this is more difficult to do with short code snippets.
- amelius 10y agoNice idea, but that must be the worst explanation of monads I've seen so far :)
- sdegutis 10y agoCan you recommend a good one? I've read several but still don't get it. And I hardly have time to write code to learn it like people say is necessary.
- seagreen 10y agoCan you recommend a good one? ... I hardly have time to write code to learn it like people say is necessary. The _easiest_ way to learn monads is through code (in haskell they're just a tricky typeclass: http://dev.stephendiehl.com/hask/#eightfold-path-to-monad-satori http://dev.stephendiehl.com/hask/#eightfold-path-to-monad-sa...) If you don't want to write code you could learn them like a mathematician from first principles (https://en.wikipedia.org/wiki/Monad_(category_theory) https://en.wikipedia.org/wiki/Monad_(category_theory)), but that seems so much harder to me I'm not sure why you would.
- pron 10y agoPrecious few mathematicians ever learn what monads are...
- Dr_tldr 10y agoIs there a non-haskell resource on monads you could recommend? Anything that starts with "first, learn haskell" is probably not the easiest way, assertions from people who already know haskell notwithstanding.
- seagreen 10y agoGreat point! I found this for python: https://www.stephanboyer.com/post/9/monads-part-1-a-design-pattern https://www.stephanboyer.com/post/9/monads-part-1-a-design-p..., but it definitely focuses more on explanation than practical use. A list of "how to practice with monads in various languages" would be a neat thing to have.
- iandanforth 10y agoThis is great. Very helpful for understanding / translation.
- toolslive 10y agowhat about homographs ? For example, 'functor' means something different depending on the programming language (Haskell, OCaml)
- eru 10y agoNot only that, oop people sometimes use 'functor' to mean an object you can call like a function.
- dexwiz 10y agoIt uses Javascript in the examples, but Javascript has slightly different definitions for some. Partial Application and .bind(). Function.bind() in JS is about setting `this` for when the function is called[1]. Constant, `const`, declaration in JS governs reference reassignment. Is that the same as referential transparency? Either way the example is wrong. The following is not an invariant. `five` cannot be changed, but `john.age` can be. The object reference is constant, but not the object value. `john.age + five === ({name: 'John', age: 30}).age + (5)` Lazy evaluation should be called a generator if we are doing JS specific naming conventions.[2] Seems like the Haskell camp like the term Lazy Evaluation, but the Pythonthoic yield camp likes the term Generator. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- cfallin 10y ago> Lazy evaluation should be called a generator if we are doing JS specific naming conventions. The two are actually orthogonal -- generators are a control-flow structure where control keeps re-entering an existing function context and returning from it (basically, a coroutine), whereas lazy evaluation is a language-semantics choice where every value starts out as a thunk (bit of code) with enough information to compute the value, until resolved to the result. Generators in the Python or JavaScript sense aren't really doing "lazy evaluation" in the Haskell sense, IMHO, though I can see how one sees the parallels (infinite sequences, etc). You can have the same "infinite sequence" by just defining an iterator implementation that always returns a next value, without the use of generators/coroutines. Likewise, there are other uses for lazy evaluation than generator-like infinite sequences. For example, lazy evaluation lets you build a (finite) table of values in a memoized dynamic-programming problem, where each value is an expression that refers to some of the other values, and the lazy evaluation semantics ensure that (i) only the needed values are computed and (ii) once a value is computed, it's memoized (the thunk resolves to its result). You get demand-based computation and memoization "for free" from the language runtime.
- AgentME 10y ago
- erikrothoff 10y agoSeriously, what's up with these comments? If the examples are plain wrong, wouldn't it be more prudent to create an issue in the repo? Also it seems to be a lot of hate for the use of JavaScript. It's a great thing that they chose the most widely available language. We get it, you know a purely functional language. Good for you! I am really disappointed in the general, holier-than-thou attitude here.
- codemac 10y agoAnd in fact, the learning of new terminology and a new syntax/libraries/etc as always made it more difficult for me to learn new languages. I now try to learn new concepts in a language I'm more familiar with, and then learn the language/syntax. I find this repo wonderful!
- vsviridov 10y agoyou might enjoy this then: https://drboolean.gitbooks.io/mostly-adequate-guide/ https://drboolean.gitbooks.io/mostly-adequate-guide/
- DeadReckoning 10y agoPeople want to feel like they're smarter than everyone else, basically.
- bhrgunatha 10y agoI think it would be even better if the repo had the javascript examples alongside another language which is more widely accepted as a functional language for example Haskell, Scala or Clojure. Seeing examples together to me is an even stronger way of illustrating the concepts and differences between implementations.
- catnaroek 10y agoA more serious objection is that many structures used in functional programming have associated equational laws, which JavaScript provides a very poor medium to formulate - never mind actually prove. The ability to query the physical identity of every object makes the equational theory of JavaScript objects completely trivial: every object is equal to itself (physically in memory) and nothing else.
- jtchang 10y agoHmm first time I had a semblance of grasping what the heck a monad was. Cool.
- catnaroek 10y agoHow come the definitions of “functor”, “applicative functor”, etc. don't mention the most important part? The laws. The definition of equational reasoning is also very weak: > When an application is composed of expressions and devoid of side effects, truths about the system can be derived from the parts. Truths about any system can be derived from the parts. What equational reasoning gives you is the ability to study the parts in isolation from each other.
- mavelikara 10y agoThis could well lead to a GoF-style book to be published. And 15 years later, cool kids then can point and laugh at "Traversable Semigroup Isomorphism" like they do for "Abstract Factory Factory" now.
- 33degrees 10y agoFor anyone looking for something more in depth, check out https://drboolean.gitbooks.io/mostly-adequate-guide/content/ https://drboolean.gitbooks.io/mostly-adequate-guide/content/
- bogomipz 10y agoNeat! Thanks for sharing.
- openasocket 10y agoSome of these are basic, some are more complex, and others I've never ever seen actually used. Does some one here actually know a practical (or at least non-toy) example usage of a co-monad?
- platz 10y agolenses, the store comonad, cellular automata, convolution,zippers. "whenever you see large datastructures pieced together from lots of small but similar computations there's a good chance that we're dealing with a comonad" http://www.haskellforall.com/2013/02/you-could-have-invented-comonads.html http://www.haskellforall.com/2013/02/you-could-have-invented... (The Builder Pattern, The Iterator Pattern, The Command Pattern)
- jethrolarson 10y agoSome of the words defined like comonad were added merely because they're defined in the fantasy-land spec. It's all still a WIP so contributions or corrections are welcome.
- DeadReckoning 10y agoWhy on earth are people complaining about using Javascript to illustrate functional programming language concepts? The whole point of this is helping non FP programmers to wrap their head around these terms. Javascript is by far the best mainstream language for doing functional programming.
- catnaroek 10y agoI'm not so sure. In C++, most certainly a mainstream language, two copies of the complex number `2 + 3i` are equal to each other. In JavaScript, they're different. (Assuming you make two objects with fields `realPart` and `imaginaryPart`.) How can you do functional programming on a foundation where not even the most basic property of equality holds (every value is equal to itself)? Remember that functions are mappings from values from a domain, into values from a codomain. A language whose treatment of compound values is as feeble as JavaScript's can't possibly constitute the right foundation for doing functional programming.
- nomel 10y agoNot knowing anything about this, is the end effect the same?
- catnaroek 10y agoThe effect is that, if you want to reason about JavaScript programs, you can't carry out your reasoning in JavaScript itself, but rather have to use an external metalanguage - ideally something like Hoare logic, but, realistically, usually an informal, insufficiently understood, even insufficiently defined language, like English. OTOH, a Haskeller can reason about Haskell programs by just evaluating Haskell expressions.
- eru 10y agoJust define your own equality operator? Eg in Haskell, whether things can be compared for equality depends on their representation. (Eg church encoded datatypes do not lent themselves to the built-in derivation of the == operator.)
- intjk 10y agoThis is great! Throwing all of these into an anki deck. :)
- ninjakeyboard 10y agoSome feedback for discussion - some of the FP stuff is sort of blurry to me after a point but here is what I sort of feel scratching this at the surface as a scala dude. Not trying to be critical - just whatever is off of the top of my head a la review. Lazy: I feel like the definition of lazy needs to be demonstrated in contrast to eager evaluation. val x = {println("evaluating x"; 5)} lazy val lazyX = {println("evaluating lazyX"; 5)} this would print ("evaluating x) to the console. lazyX would not be evaluated because it is not invoked. Here the eager definition of x demonstrates that it is evaluated immediately. lazyZ is only evaluated when invoked. Monads and higher kinded types. The informal vs formal definition of some of the higher kinded types is worth noting. You're either describing the most formal definitions (identity/associativity laws) or you're describing the informal defition (has flatmap, processing things in a series as described in the wikipedia article https://en.wikipedia.org/wiki/Monad_(functional_programming) https://en.wikipedia.org/wiki/Monad_(functional_programming) ) Monad seems to be the informal definition, otherwise associativity laws should be included maybe? Eg in Scala Try is not a formal monad as the associativity laws don't hold true. https://wiki.haskell.org/Monad_laws https://wiki.haskell.org/Monad_laws Also why is it "of" and "chain" here? Isn't it traditionally flatmap that indicates a type might be a monad, and then the identity laws the truly confirm it? Maybe this is js land? If so, should the doc say "this is js" as flatmap is pretty descriptive of map+flatten. It's hard to talk about - It's not at all clear to me what people mean when they talk about monads either - just throwing thoughts around. I think you should outline how Currying and Partial Application are related maybe? Eg currying is taking a function with an arg list and making it take multiple arg lists (the act of making a function take multiple arg lists), and Partial application is the application of an argument of a curried function more or less? I think the relationship between these should be highlighted possibly to sort of draw out what part of the life of a function we're talking about if trying to illuminate the subject for someone.
- jethrolarson 10y agoThanks for the feedback. I've been trying to direct the definitions to be informal and understandable but many contributions come in with a different voice. Some of the definitions are first draft attempts just to get something in place. It's been awesome to see the sudden popularity of our humble glossary :)