functional-programming | Introduction to Functional Programming using TypeScript | Functional Programming library
kandi X-RAY | functional-programming Summary
kandi X-RAY | functional-programming Summary
Functional Programming is programming with pure functions. Mathematical functions.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of functional-programming
functional-programming Key Features
functional-programming Examples and Code Snippets
Community Discussions
Trending Discussions on functional-programming
QUESTION
From Real World Haskell I read
It operates as follows: when a
seq
expression is evaluated, it forces its first argument to be evaluated, then returns its second argument. It doesn't actually do anything with the first argument:seq
exists solely as a way to force that value to be evaluated.
where I've emphasised the then because to me it implies an order in which the two things happen.
From Hackage I read
The value of
seq a b
is bottom ifa
is bottom, and otherwise equal tob
. In other words, it evaluates the first argumenta
to weak head normal form (WHNF). seq is usually introduced to improve performance by avoiding unneeded laziness.A note on evaluation order: the expression
seq a b
does not guarantee thata
will be evaluated beforeb
. The only guarantee given byseq
is that the botha
andb
will be evaluated beforeseq
returns a value. In particular, this means thatb
may be evaluated beforea
. […]
Furthermore, if I click on the # Source
link from there, the page doesn't exist, so I can't see the code of seq
.
That seems in line with a comment under this answer:
[…]
seq
cannot be defined in normal Haskell
On the other hand (or on the same hand, really), another comment reads:
The 'real'
seq
is defined in GHC.Prim asseq :: a -> b -> b; seq = let x = x in x
. This is only a dummy definition. Basicallyseq
is specially syntax handled particularly by the compiler.
Can anybody shed some light on this topic? Especially in terms of:
- What source is right?
- Is
seq
's implementation really not writable in Haskell?- If so, what does it even mean? That it is a primitive? What does this tell me about what
seq
actually does?
- If so, what does it even mean? That it is a primitive? What does this tell me about what
- In
seq a b
isa
guaranteed to be evaluated beforeb
at least in the case thatb
makes use ofa
, e.g.seq a (a + x)
?
ANSWER
Answered 2021-Apr-04 at 18:21Real World Haskell is mistaken, and all the other things you quoted are correct. If you care deeply about the evaluation order, use pseq
instead.
QUESTION
Say you have this in an Object-Oriented application:
...ANSWER
Answered 2021-Mar-09 at 02:26Functional programming languages have a variety of ways of achieving polymorphism. I'm going to contrast Java (the OOP language I know best) with Haskell (the functional language I know best).
Way 1: "parametric polymorphism"
With parametric polymorphism, you don't need to know anything at all about the underlying type. For example, if I have a singly-linked list with elements of type T, I actually don't need to know anything about type T in order to find the length of the list. I would just write something like
QUESTION
I'm having trouble understanding why the following code returns an error:
...ANSWER
Answered 2021-Feb-22 at 15:05The second form you've linked to doesn't make much sense, I would avoid it.
If I rewrite your function like this it seems to work:
QUESTION
Sorry, I don't think the title I wrote is correct and explains well what I'm asking, but at the moment I have not found better
I'm trying to change my approach from imperative to functional-programming in Java, sometime is easy but in other case no much... But I'm not giving up :)
I have a map of values that should be decreased of an amount calculated by a function, and for each input the are one or more functions that could be applied, what has been decreased by the previous function is "visible" to the next function, so something like that
...ANSWER
Answered 2021-Feb-21 at 18:19A "brute-force" solution can look as follows:
QUESTION
I am trying to implement the game 2048 using JavaScript. I am using a two-dimensional array to represent the board. For each row, it is represented using an array of integers.
Here I am focused on implementing the merge left functionality i.e. the merge that happens after the user hits left on their keyboard.
Here are a set of test cases that I came up with
...ANSWER
Answered 2021-Feb-15 at 04:02You can try this.
QUESTION
How to implement a functional-programming style of exception-processing with Spring-Integration ?
i.e. instead of letting the exception bubble up to the caller or to send it down to a dedicated message-channel, have the error-prone component (e.g. gateway, router etc...) wrap the result/exception with a Option/Either construct and continue to process the rest of the integration-flow.
For example if my integration-flow looks like below, how to wrap the gateway component to pass an Either to the following transformer ?
ANSWER
Answered 2021-Jan-04 at 15:49Add an ExpressionEvaluatingRequestHandlerAdvice
(or a custom advice) to each endpoint.
QUESTION
I'm struggling with understanding the difference between functional and imperative programming. From reading https://www.sitepoint.com/what-is-functional-programming/ I see that there are a number of principles in functional programming that I use all the time in what I thought was an imperative programming.
I've read that functional programs use pure functions, so does that mean every time I make or use a pure function I'm writing in the functional paradigm?
I've also passed functions in and used them as first class objects, does that mean I was writing in the functional paradigm?
I pretty much use all of the functional paradigm principles in my code, but I never thought I was doing functional programming. Is the act of using any of these functional programming principles considered the functional programming paradigm?
...ANSWER
Answered 2020-Dec-07 at 16:59Functional principles are just techniques and ideas. These are the bread and butter of the functional programming paradigm, which is what happens when you take these tools and use their unique advantages to gain systemic advantages.
A pure function is just a function with no side effects. You've written a million of these. But now, if you write only pure functions, your app can be split across processing cores with no effort or risk.
You've used constants before. But if you almost always use constants, then the things that are variable are the only things you have to think about when tracing code, and that is quite an advantage.
And you've chained functions before, but when you make everything pipe-able your entire language begins to feel like wiring up data flows, rather than giving the computer step-by-step instructions. This is much easier for humans to reason about and is less error-prone.
The techniques always have their advantages. When they become baseline assumptions, those advantages multiply. That's the functional paradigm.
Moving my comments here for clarity:
good question! In this article medium.com/@charlesbailey333/… it talked about how Rust had advantages over C++ because it incorporates functional programming ideas better. The evidence they give is that it supports Map, Reduce, and Filter. It almost seems like they're saying that those functions are "functional programming functions", but I don't think those functions are anything special. – Joshua Segal 16 mins ago
Okay great! This I can help with. SO this author is struggling to use the actual term for what they're referencing. It's called "expressiveness". Basically it means how close is the code I'm writing to the mental model of what I'm doing? For example, you want to give someone directions on how to get from A to B. Ideally, you do this by expressing it in turns and street names. However, if your language forces you to express this using the angle of the accelerator pedal and the angle of the steering wheel, this is much clunkier to do. C++ did it clunky. Rust did it elegantly and expressively.
In general, the functional and declarative languages tend to be much better at "expressing" your ideas in code and visually. You have branching paths? Your code literally looks like a branching tree. You have a data flow with a transformer? Guess what friend, that's just a function that transforms X to Y and a some sort of pipe operator that takes care of looping and new info.
The thing is, expressiveness isn't a statistic or something you can optimize for. It's an emergent "feeling" when using the language. The paradigms are general principles that tend to be internally consistent that produce useful "feelings". FP feels like flowing pipes and transformations. OOP feels like gadgets and features that talk to each other. The different mental models have different uses. FP is better for data processing. OOP can be good for UI and stateful services. At the boundaries they can clash a little, which is where the clunk comes from in C++.
At this point anything that is "completely OOP" or "completely FP" is usually shit, to be frank, so it can be a little hard to see the identities of the two when they are so merged. If you do complete OOP you can't compose anything and you have to write a million connector classes. If you do complete FP you can't modify state or have side effects (like... uh... showing stuff on screen?). These are genres. What makes something a house beat? If something else uses a house beat is it automatically house music? Does anyone care about the categorization?
QUESTION
I started with an ES6 Class
...ANSWER
Answered 2020-Dec-04 at 22:33There's not a specific "right FP way" to do this. It's about incorporating various techniques that serve your purpose. Here, I'll use the fp-ts
library to
show you the concepts of
the IO monad (to model a synchronous effect, bc your function is called "....DoesSomething...", so we imagine it's writing a Foo to disk)
functional composition (using
flow
here, which turns a bunch of functions into a single function)immutability (immutable data reduces the possible errors you can encounter by ensuring the data is always as you expect)
lenses, in the category of optics, which deal with focusing on a single prop in an object and are basically a pair of getter and (immutable) setter functions
QUESTION
I attempt to implement a currying funtion similar to Functional Programming Jargon in Rust, which introduces code like:
...ANSWER
Answered 2020-Sep-22 at 21:44impl Trait
syntax can only be used in argument position or return position of a function signature. Both of these are fine:
QUESTION
Functional Programming in C++, at page 214, with reference to an expected
monad which is the same as Haskell's Either
, reads
[...] as soon as any of the functions you're binding to returns an error, the execution will stop and return that error to the caller.
Then, in a caption just below, it reads
If you call
mbind
[equivalent to Haskell's>>=
] on anexpected
that contains an error,,mbind
won't even invoke the transformation function; it will just forward that error to the result.
which seems to "adjust" what was written earlier. (I'm pretty sure that either LYAH or RWH underlines somewhere that there's no short-circuiting; if you remember where, please, remind me about it.)
Indeed, my understanding, from Haskell, is that in a chain of monadic bindings, all of the bindings happen for real; then what they do with the function passed to them as a second argument, is up to the specific monad.
In the case of Maybe
and Either
, when the bindings are passed a Nothing
or Left x
argument, then the second argument is ignored.
Still, in this specific two cases, I wonder if there's a performance penalty in doing something like this
...ANSWER
Answered 2020-Sep-17 at 19:32Consider the following expression:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install functional-programming
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page