Supports OCaml monads
Two good reasons can be given as an answer: On the one hand, languages like Haskell or PureScript are not exactly known for their flat learning curve. For example, Haskell does not allow direct side effects, so developers often fall back on monads that are reputed to be difficult to understand. This culminates in the legend of the so-called monad curse, which says: "The moment someone understands monads, the person loses the ability to explain monads".
Regardless of whether monads are really that complicated, the deterrent effect cannot be denied. ReasonML allows monads, but understanding the theory behind them is not required and they are not a typical part of the language or its documentation. For example, ReasonML allows side effects as normal and does not require pure functions like Haskell. In general, the ReasonML documentation tries to avoid too much theory and extreme jargon. Anyone who comes across zygohystomorphic prepromorphisms in the Haskell documentation may appreciate this.
The StringCalculator-Kata (programming exercise) by Roy Osherove should serve as a small example for functional programming and the combinability of functions.
The task is to add a string of comma-separated numbers like "1,2,3" to a sum - in this case to 6. The calculation should only take numbers smaller than 1000 into account. Therefore the following result would be correct:stringCalc ("1,1005,2") => 3
The disadvantage of the code is that the different aspects of the calculation are interlinked and the individual steps of the algorithm can only be identified with great difficulty.
Functional programming usually approaches the calculation with so-called pipelines:
In this code, every single step can be clearly seen and the individual aspects are neatly separated from one another. The first thing to do is to split the string using commas, then convert the individual strings into integer values and only take numbers smaller than 1000 into account. At the end there is a totalization.
Now it is still a matter of programming the individual functions such as and.
=> Array.to_list (Js.String.split (",", str));
The legibility already suffers from the nesting and the many brackets. The pipe operator can help. It uses the result of a function call as the (last) argument of the next. Therefore the function can be written as follows:let splitByComma = str
=> Js.String.split (",", str) |> Array.to_list;
or even aslet splitByComma = str
=> str |> Js.String.split (",") |> Array.to_list;
The code is easier to read: The function receives the string, splits it up and then converts the result array into a list. The reading direction runs from left to right without nesting, as it is the usual way of reading texts.
should convert the individual strings - in the example, and) into suitable values.
can be implemented with the classic filter function and a lambda:let lessThan1000 = List.filter (x => x <= 1000);
Thanks to currying, it is not necessary to explicitly mention the list argument.
"folds" the list into a single value. ReasonML understands an operator in brackets as a function. This is a shorthand way of writing. Finally, the second argument is the starting value of the sum.
- How can I fix iTunes error 3600?
- Where should I visit in Bangladesh
- How is a graduated cylinder measured
- Offers BITS Pilani petroleum engineering
- Does the mailbox have points
- Why are gases insulators at room temperature
- Are alkyl halide esters of hydrochloric acids
- What developments have tape recorders undergone?
- What is the longest running film
- How do you research without Google
- What is social practice
- All therapists have insight
- What are the best Bollywood weddings
- What is the relationship between C and C ++
- Can a software developer afford a BMW?
- Which IIIT is good
- Where are most of the software bugs introduced
- How does yoga help digestion
- Why doesn't Windows Media Player open?
- What does cabbage mean
- If self-help doesn't work, what then
- Have you already visited my website?
- Feel like a mechanical engineer
- Why is the Indian rupee doing so badly?