Hello! I am the author of MojiScript (a functional re-imagining of JavaScript) and I would like to help programmers who may be functional-curious :)
Have questions about functional programming but were afraid to ask?
Post questions here or @ me on twitter @joelnet use the hashtag #fpquestion.
I'll do my best to answer your questions and if I don't know, I'll try to point you in the right direction. If I can't do that, I'm sure someone smarter than I am will swoop in to save the day.
Don't bother asking "What is a Monad?" because everyone knows:
A monad in X is just a monoid in the category of endofunctors of X
If you have a code question, like "How do I do XXX" and you post source code, please make sure the code is runnable and make sure the code is boiled down to as small as you can make it. I can't convert your 1,000 file for you :)
Example of a good question
How do I do this...
for (let i = 1; i < 6; i++) {
setTimeout(() => console.log(i), 1000)
}
Oh and let's have fun!
My articles are very Functional JavaScript heavy, if you need more FP, follow me here, or on Twitter @joelnet!
More articles
Let's make a DEV.to CLI... together
Let's talk about auto-generated documentation tools for JavaScript
Top comments (32)
I have two functions:
getUserFromDB :: Token -> Either NotFound User
and
getNewUser :: Token -> Either NoMoreInfo User
The Token is a token from OAuth api response.
getNewUser
uses it to get more info from api and create new user in my DB. andgetUserFromDB
just gets a user from DB with the help of token info. The problem is: how do I combine these two functions into one likegetUser :: Token -> Either OneOfTwoPossibleErrors User
, which will first try togetUserFromDB
feeding token to it and then, if it fails, try togetNewUser
again feeding the same token to it? FP is very good at creating consecutive streams that process data, but how to create two or more parallel pipes that are tried in sequence until one of them returns good result, and return any result from the last one if none succeeds?It depends on the library you are using for the Either. But maybe these docs from Folktale's Either can help: folktale.origamitower.com/docs/v2....
There's a
leftMap
method that will run when the either is a Left. So it would look likeThanks! Good to know. But what about more general way, for example if I had a list of functions to try in order, like
[getUserFromDb, getNewUser, trySmthElse, ....]
? And I don't want to map that list into a list of results because that would mean execution of all of them, and I don't need a new user ifgetUserFromDB
had found the one I needed.Maybe something like this?
Isn't
leftMap
, as a function (not a method.leftMap
) takes anEither
as its first argument? And to put up a pipe ofleftMap
s withEither
s I would need to first get all theEither
s and that would mean running all the functions again, which I try to avoid.There is a function
until
in PureScript that is the closest to what I'm looking for:until :: ( a -> Boolean ) -> m a -> m a
, butm
isMonad
, and I don't quite get yet how I can put a list of functions in it so that they will all get the same input value and be run one by one and tested after each run.until
then returns am a
that is also can just be a list of results, kind of likemap
would produce, but that list will end as soon as(a -> Boolean)
evaluates totrue
. Then I could justlast
on thatm a
result and, according tolast
definition, -> maybe get a successful output from the first succeeded function in the list =)I think I found a somewhat simple solution!
Ahh okay. I see what you are trying to do. Nice solution!
What a great question!
Fortunately I have already written an entire article on this subject with Ramda!
I ā¤ Ramda - Partial Application with a Special Placeholder
What is the best way to introduce a mostly FP codebase to newly hired junior developers?
Great question and difficult to answer. It would highly depend on how the codebase is written. For example, a MojiScript application I would start with explaining the
pipe
, because your app will be verypipe
heavy.But as a general introduction to FP, I like to start with
map
,filter
, andreduce
.Show patterns imperative patterns they are used to with FP patterns used in the codebase. Something like this: Functional vs Imperative Patterns in JavaScript
Immutability is a great subject.
And I also love function composition!
JavaScript AllongƩ, the "Six" Edition is one of my favorite books. You can read it free at that link. It starts out pretty easy and goes very deep. So you can get something out of it at every level.
What is your current process for onboarding?
We're in a new stage of growth, adding developers for the first time in a while. I.e., we are starting anew with this process! Any feedback for onboarding is welcome. In fact, I think someone wrote about it recently.
I have been contemplating creating a series on functional programming, targeted towards junior and new developers. Would this be something you would find interesting?
Wouldn't everyone? Ha!
In certain use cases FP is also faster.
Redux is FP and the majority of the React community has no problems with it.
When you are performance testing your code, the slowdown will not be cause by a
map
vs afor
loop. The slowness will come from an Ajax call, reading a file, access a database and not from the code itself.I would say put human readability and code-reusability above fast code. Optimize after you need to optimize. Do not prematurely optimize.
Do not let this myth hold you back.
I would recommend watching this video that tackles this subject:
Hi joel,
can you explain the diff between curry and partial function application?
thanks
This is a great question!
First let's define
arity
as the number of arguments a function takes. This function is 2 arity.Currying is taking a 2+ arity function and splitting it into many 1 arity functions.
So if we were to curry
add
, it would look like this:Now I would call the
add
function like this:Partial application is used with curried functions to partially apply arguments.
But partial application is not specific to curried functions either. For example, you could partially apply any non-curried function using
bind
.I hope that has been helpful!
Cheers!
Could you explain the state monad for me?
Not well. I have never used it.
The state monad is a lazy state manager. I could see it being useful when passing to functions as they would lazily update the state. The state wouldn't then be computed until you need to compute it, which wouldn't happen inside of the pure function.
It's also nice that it returns the computed value and the state as a pair. I feel like this could be useful, but I haven't really thought of a good use case for it yet.
I have read a bunch of stuff on the state monad and don't understand any of it.
The only thing I did understand was this, which might also be helpful to you:
State Monad in JavaScript
I'm not so much of a stickler for this level of purity and algebraic state management. When I need state, I'll usually grab redux.
I would compare it to a be redux reducer.
Cheers!
How do you write something like
websocket.onmessage
orevent-emitter.on
in a functional way?More question will come when I am on my notebook š .
Thanks for starting this thread.
He means me.
Obvi!
ask away!
Some comments may only be visible to logged-in visitors. Sign in to view all comments.