These are chat archives for ramda/ramda

9th
Feb 2017
Kurt Milam
@kurtmilam
Feb 09 2017 09:14
@zwc This should do the trick: https://goo.gl/qtrRn1
``````const result = { visits: [ 'a', 'b', 'dup', 'c', 'dup' ] }

const findFirst = ( acc, x ) =>
ifElse( compose( contains( x ), tail )
, tail
)( acc )

reduce( findFirst, result.visits, result.visits ) // -> 'dup'``````
Bernhard Wang
@zwc
Feb 09 2017 09:16
Cool, I'll try to understand it :)
@kurtmilam : Works perfectly ;-)
Kurt Milam
@kurtmilam
Feb 09 2017 09:19
1. It checks whether the `tail` contains the `head`
2. If it does, it sets the accumulator to`head` and short circuits the `reduce` with `reduced`
3. Otherwise, it sets the accumulator to `tail` and moves on to the next item in the list
Cool. I wrote that looking for a solution to https://adventofcode.com (day one, puzzle 2).
Bernhard Wang
@zwc
Feb 09 2017 09:20
Hahaha, that's exactly what I am using it for too. I'm going through all the challenges, replacing my "old" code with Ramda. Just as a way of learning it.
Kurt Milam
@kurtmilam
Feb 09 2017 09:23
Figured that was a possibility :)
Here's my day 1, puzzle 1 solution: https://goo.gl/B2Fql0
Bernhard Wang
@zwc
Feb 09 2017 09:26
Kurt Milam
@kurtmilam
Feb 09 2017 09:29
Looks good!
Aldwin Vlasblom
@Avaq
Feb 09 2017 09:32
I had my compass like this:
``````const North = 0;
const East = 1;
const South = 2;
const West = 3;

const Left =  -1;
const Right = +1;

const turn = (from, to) => (4 + (from + to)) % 4;

turn(West, Right) === North
turn(North, Left) === West
turn(East, Right) === South``````
Kurt Milam
@kurtmilam
Feb 09 2017 09:35
@Avaq I had a similar compass to yours, but wanted to see whether I could make working compass with a doubly-linked cyclical list and lenses :)
Feb 09 2017 09:54
Hi all. A quick question: would it be ok to post a job advert here? Regardless, would you be able to suggest how\where to look for js developers with strong functional background?
James Forbes
@JAForbes
Feb 09 2017 10:58

Hi. I don't know if something already exists, and I can't speak for the project, but I've seen some projects with a jobs section on their wiki; companies/developers can open PR's to add themselves to the list. Maybe you could make a proposal for something like that in an issue @adrian-gierakowski and see if there's any objections?

Maybe there's something already set up, I've never seen it before though. Good luck finding devs

Markus Pfundstein
@MarkusPfundstein
Feb 09 2017 11:46
Hey guys. Can someone tell me what the best way is to write a (while / recursive loop) with native Promises?
James Forbes
@JAForbes
Feb 09 2017 12:50

``````const Promise = require('bluebird')

Promise.coroutine(function * () {
while( true ){
yield somePromise()
}
})``````

You can also do something like this

``````something()
.then(function(){
if( condition ) {
return something()
}
})``````
Feb 09 2017 13:34

Hi All,
I'm trying to compose something like this:

``args => tryCatch(doSomething(args), reportError(args))``

is there a simple way to do this without actually writing out the whole function?

Rick Medina
@rickmed
Feb 09 2017 13:42
@eladzlot do you mean a point free version (without explicit "args")?
Feb 09 2017 13:43
yes
Rick Medina
@rickmed
Feb 09 2017 13:55
R.tryCatch returns a function that applies the argument passed in to both functions automatically, that is in general how ramda works. Try R.tryCatch(doSomething, reportError)(someInput)
Feb 09 2017 14:01
thanks @rickmed
Rick Medina
@rickmed
Feb 09 2017 14:02
@eladzlot *I should say: ramda returns curried functions that does what its signature describes
Galileo Sanchez
@galileopy
Feb 09 2017 15:08
Promises are a nice way to encapsulate values that are not yet available, however they could also imply and be used for building computations, and I'd like to know if that is a proper use case or is it abusing a Promise? And if it is infact abusing promises then what would be a good way to describe a self running computation? like a daemon that checks a value every X seconds returns the value and start again?
Denis Stoyanov
@xgrommx
Feb 09 2017 15:22
promises are bullshit
@galileopy rx/most/kefir/bacon/flyd ...
Galileo Sanchez
@galileopy
Feb 09 2017 15:33
[Galileo Sanchez, CafeinaSW] Bacon is nice, I'm asking in more general ways, I do see how I can build the same thing with bacon, however this doesn't answer the question about representing computations with data structures that have the implied context of beign data wrappers. I do have in mind a sort of where is it more appropriate to use something like Data.Task instead of a Promise since both can achieve the same result in different ways. Or am I just blatanlty confused by trying to separate description of computations from a Promise, or any other data structure that can accomplish similiar results?
Denis Stoyanov
@xgrommx
Feb 09 2017 15:37
@galileopy do u need some timer and operation by this timer?
Galileo Sanchez
@galileopy
Feb 09 2017 15:40
Yes, and I know that I can make it by starting a EventStream that ticks every X seconds and map that stream to the values I need to check, and then do what I need to do according to the results
But somehow it avoids the question completely? hmm it actually gives a nicer solution to the same problem, and the question was basically "What is more appropriate to solve X task", if A is a nicer solution than B to solve X then A should be the answer in this case.
@xgrommx you do have a talent to see what's the idea behind words, thanks!
Rick Medina
@rickmed
Feb 09 2017 16:03
@xgrommx has no patience for promises :joy:
Denis Stoyanov
@xgrommx
Feb 09 2017 16:04
@rickmed :smile:
James Forbes
@JAForbes
Feb 09 2017 16:53
is there a name for a flipped `K` combinator? A function that takes 2 arguments and returns the 2nd?
Denis Stoyanov
@xgrommx
Feb 09 2017 16:58
@JAForbes `K(I)`
@JAForbes `always(identity)`
James Forbes
@JAForbes
Feb 09 2017 17:02
oh nice!
Thanks @xgrommx
that's so logical
Rick Medina
@rickmed
Feb 09 2017 17:08
for any outsider those last 2 comments would sound sooo sarcastic! :smile:
"K(I)" so obvious!
Denis Stoyanov
@xgrommx
Feb 09 2017 17:26
@rickmed because K and S are axioms of logic
Rick Medina
@rickmed
Feb 09 2017 18:31
@xgrommx it's on my task list; study my combinators
Denis Stoyanov
@xgrommx
Feb 09 2017 20:25
port several functions of ramda to haskell
``````reduceBy :: forall t (t1 :: * -> *) a t2. (Foldable t1, Ord t2) => (a -> t -> t) -> t -> (a -> t2) -> t1 a -> Map t2 t
reduceBy valueFn valueAcc keyFn
= Prelude.foldr
(\elt -> M.alter (Just . valueFn elt . (\case { Nothing -> valueAcc; Just v -> v})) (keyFn elt))
M.empty

countBy :: (Foldable t1, Ord t2, Num t, Enum t) => (b -> t2) -> t1 b -> Map t2 t
countBy = reduceBy(const succ) 0

groupBy :: (Foldable t1, Ord t2) => (a -> t2) -> t1 a -> Map t2 [a]
groupBy = reduceBy(:) []

indexBy :: (Foldable t1, Ord t2) => (Map k a -> t2) -> t1 (Map k a) -> Map t2 (Map k a)
indexBy = reduceBy const M.empty

example = indexBy (M.lookup "id") [M.fromList [("id", "xyz"), ("title", "A")], M.fromList [("id", "abc"), ("title", "B")]]``````
Drew
@dtipson
Feb 09 2017 21:28
@galileopy imo, Promises are a poor abstraction for computations because as you say, they're really an attempt to create "values" (but specifically, stateful ones) and then allow chained, derivable states. A Task or a Future is a better model for a computation that may or may not take time to generate an effect, because it's just a composable description of the computation. Check out the .chainRec method on Flutures https://github.com/fluture-js/Fluture#chainrec
Drew
@dtipson
Feb 09 2017 21:41
You can of course do all your async work in Streams even if you only have one value just like you can stick a single value in an Array and do some Functory-work with it. But Tasks/Futures are better suited/more direct for the situation where you're generating a single value and want to work over it (and possibly some of that work will also be async, for which you can use .ap and .chain)
Galileo Sanchez
@galileopy
Feb 09 2017 22:36
@dtipson thanks for the reference to flutures