These are chat archives for ramda/ramda

21st
Apr 2017
Stephan Meijer
@smeijer
Apr 21 2017 14:13
const value = ['a', 'b', 'c']; // ['a', 'b', 'c'] || 'a';
const selector = (typeof value === 'string') ? value : { $in: value }
I'm convinced that there must be an awesome Ramda oneliner to fix this. But I cannot come up with one. Some thoughts on this?
Michael Rosata
@mrosata
Apr 21 2017 14:32
@smeijer Could you expand a bit? If you were writing a couple tests, what would be the inputs and expected outputs for the tests?
Matthew Willhite
@miwillhite
Apr 21 2017 14:40
unless(is(String), objOf(‘$in’), value)
Something like that perhaps?
Stephan Meijer
@smeijer
Apr 21 2017 14:40
@mrosata sure, it's to create mongo selectors:
const parse = val => (typeof val === 'string') ? val : { $in: val };

parse('a'); // => 'a'
parse(['a', 'b']); // =>  { $in: ['a', 'b'] }

db.find({ _id: 'a' });
db.find({ _id: { $in: ['a', 'b'] } });
@miwillhite , thanks. objOf was the missing part :sunglasses:
Michael Rosata
@mrosata
Apr 21 2017 14:43
@smeijer yea I think @miwillhite has it with unless(is(String), objOf($in))
Stephan Meijer
@smeijer
Apr 21 2017 14:44
const parse = R.when(R.is(Array), R.objOf('$in'));

parse(null); // => null
parse('a'); // => 'a'
parse(['a', 'b']); // { $in: ['a', 'b'] }
that's how I need it :heart_eyes:
Michael Rosata
@mrosata
Apr 21 2017 14:45
:thumbsup:
Michael Rosata
@mrosata
Apr 21 2017 15:02
Out of curiousity, can anyone figure out a shorter way to write either of these capitalize or titleCase functions?
    const [unwords, words] = [join(' '), split(' ')]
    const capitalize = converge(concat, [compose(toUpper, head), tail])
    const titleCase = compose(unwords, map(capitalize), words)
    titleCase('hello there, my friend')  // "Hello There, My Friend"
some reason I can't figure out markdown today
Shane Keulen
@SeeThruHead
Apr 21 2017 15:22
i don't know that this is any better but
const mapStringHead = R.curry(R.pipe(R.over(R.lensIndex(0), R.__), R.join('')));
const capitalize = mapStringHead(toUpper);
const titleCase = R.pipe(R.split(' '), R.map(capitalize), R.join(' '));
or
const mapStringHead = R.curry(R.pipe(R.over(R.lensIndex(0), R.__), R.join('')));
const capitalize = mapStringHead(toUpper);
const titleCase = R.replace(/\w\S*/g, capitalize);
Michael Rosata
@mrosata
Apr 21 2017 15:45
@SeeThruHead thanks, I like the use of replace better than composition
Aaron Mc Adam
@aaronmcadam
Apr 21 2017 16:06
Does anyone use Rx here? I'm curious about the flatten() operation, it looks like it kicks off the side-effects
Rick Medina
@rickmed
Apr 21 2017 17:02
I don't remember a flatten operator in rxjs. Maybe it's from xstream?
probably akin to join or rxjs mergeall
Ryan Stegmann
@rstegg
Apr 21 2017 17:34
can there be a psa about flat/fold/[ambiguously defined term] someday?
Robert Mennell
@skatcat31
Apr 21 2017 17:42
@rstegg context removal(fold the box) or recursive reduction(reduce in JS)?
Ryan Stegmann
@rstegg
Apr 21 2017 17:42
@skatcat31 exactly :smile:
i refer to the lack of consistency in the usage
Robert Mennell
@skatcat31
Apr 21 2017 17:45
@rstegg that's really more a problem of everyone designing ideas atthe same time and instead of calling it 'recursive reduction' or 'context removal' going 'I think I'll be clever and call it fold, because it's like it's folding in on itself!'
Ryan Stegmann
@rstegg
Apr 21 2017 17:46
sounds like a job for a psa! :smile:
Robert Mennell
@skatcat31
Apr 21 2017 17:46
however the one consistent thing I've noticed is that fold seems to mean 'Take container, and remove values through some function'
so if we go with that assumption, fold then applies ot context removal regardless of type. However the type determines HOW to remove it from context
Ryan Stegmann
@rstegg
Apr 21 2017 17:48
fold isn't as big of a mess as flat* seems to be
or rather, the interchanging of flatmap and various terms
Robert Mennell
@skatcat31
Apr 21 2017 17:53
AFAIK flat means 'take this opperation, flatten results'... so on a list of lists, map over the lists of lists, and flatten the results into a single list
Rick Medina
@rickmed
Apr 21 2017 17:53
problem I've seen with the monadic compositor is that some people name it operationally and others semantically, nevermind the name conflicts (bind in js)
so you get a zoo of terms: flatmap, chain, selectMany, bind, >>= ...
Robert Mennell
@skatcat31
Apr 21 2017 17:55
@rickmed I thought chain was more like take functor, fold, pass to function? (chain = fn => fn(this.fold()))
Ryan Stegmann
@rstegg
Apr 21 2017 17:56
but flatmap and fold/unfold are also commonly used interchangeably :smile:
Robert Mennell
@skatcat31
Apr 21 2017 17:57
unfold still confuses the heck out of me...
Rick Medina
@rickmed
Apr 21 2017 17:57
what?? where?
@rstegg
Robert Mennell
@skatcat31
Apr 21 2017 17:57
:point_up:
Rick Medina
@rickmed
Apr 21 2017 17:57
@skatcat31 chain as in fantasy-land?
Ryan Stegmann
@rstegg
Apr 21 2017 17:57
sorry i mispoke
flatmap and chain*
Robert Mennell
@skatcat31
Apr 21 2017 17:59
@rickmed pretty much
Rick Medina
@rickmed
Apr 21 2017 17:59
semantically they are the same
implementations may differ
also, join + map
hence, flatmap
Robert Mennell
@skatcat31
Apr 21 2017 18:01
... How does an iterative/list impliment chain?
Ramda does address it's chain is actually a flatmap
Rick Medina
@rickmed
Apr 21 2017 18:03
yeap
that's what I mean semantically vs operationally
James Forbes
@JAForbes
Apr 21 2017 18:04
The name chain has really grown on me.
Rick Medina
@rickmed
Apr 21 2017 18:04
:point_up_2:
Robert Mennell
@skatcat31
Apr 21 2017 18:04
@rickmed is it just me or does fantasy-land's apply look a lot like other people fold?
or am I just massively misreading it?
Rick Medina
@rickmed
Apr 21 2017 18:08
I've seen fold's implementations as FL's ap (exactly as the old api where the container had the value)
Robert Mennell
@skatcat31
Apr 21 2017 18:09
we need to stop calling things fold and start calling them 'context removal`
so that should mean ap is context removal and application
Rick Medina
@rickmed
Apr 21 2017 18:11
@skatcat31 when you have the time
unfold still confuses the heck out of me...
Robert Mennell
@skatcat31
Apr 21 2017 18:14
@rickmed Is that Brian Lonsdorf?
Rick Medina
@rickmed
Apr 21 2017 18:14
yes
Ryan Stegmann
@rstegg
Apr 21 2017 18:14

lol youtube comments:

"ask questions, i won't make you feel stupid" ... so fucking arrogant

his podcast with hardy jones
Rick Medina
@rickmed
Apr 21 2017 18:15
:point_up: awesome and funny
Robert Mennell
@skatcat31
Apr 21 2017 18:16
found out about it when only after Hardy left where I work. If you get the chance, meet the two guys. Hardy's source code is what got me digging deeper into FP
oh hey wait hey @joneshf
... probably not on gitter though XD
Rick Medina
@rickmed
Apr 21 2017 18:18
he's sometimes in the FL room
Ryan Stegmann
@rstegg
Apr 21 2017 18:20
where's the room about fold?
Robert Mennell
@skatcat31
Apr 21 2017 18:20
@rstegg I want to laugh at that but I can't tell if it's a throwback joke or not...
Ryan Stegmann
@rstegg
Apr 21 2017 18:20
it is :worried:
Robert Mennell
@skatcat31
Apr 21 2017 18:21
@rstegg :laughing:
Ah ambiguity
Rick Medina
@rickmed
Apr 21 2017 18:21
first result is the folktale room :)
Robert Mennell
@skatcat31
Apr 21 2017 18:24
not fantasy-land?
Denis Stoyanov
@xgrommx
Apr 21 2017 19:32
F-{co}algebra :heart:
Gabe Johnson
@gabejohnson
Apr 21 2017 20:41
@rickmed back on the chain train again?
Rick Medina
@rickmed
Apr 21 2017 20:51
@gabejohnson can't escape it. "Monads everywhere..."
insert gif
Gabe Johnson
@gabejohnson
Apr 21 2017 20:54
hahaha
It's raining monads
Bravi
@Bravilogy
Apr 21 2017 21:19
guys, if you know, how can I write data to a pdf file using node's fs ?
I tried fs.writeFile('name.pdf', data, 'base64') but that doesn't work
Brad Compton (he/him)
@Bradcomp
Apr 21 2017 21:51
I would think you'd need some library that is familiar with how to encode a PDF, and then you would want to write out the bytes directly
Rick Medina
@rickmed
Apr 21 2017 23:05
@xgrommx is there such thing as a recursion scheme hierarchy? akin to the algebraic structure hierarchy?
Denis Stoyanov
@xgrommx
Apr 21 2017 23:09
yes
Rick Medina
@rickmed
Apr 21 2017 23:14
thanks
Rick Medina
@rickmed
Apr 21 2017 23:25
@xgrommx is unfold a F-coalgebra or what is F?
Denis Stoyanov
@xgrommx
Apr 21 2017 23:26
@rickmed F is Functor
Rick Medina
@rickmed
Apr 21 2017 23:28
@xgrommx are catamorphisms et al restricted to functors?
Denis Stoyanov
@xgrommx
Apr 21 2017 23:29
@rickmed cata=foldr, ana=unfold
Rick Medina
@rickmed
Apr 21 2017 23:39
@xgrommx right. Nevermind, I reviewed a part of the video above and figured it out :)
Denis Stoyanov
@xgrommx
Apr 21 2017 23:40
@rickmed just try it :smile: it is awesome (I talk about RS of Edward Kmett)
Rick Medina
@rickmed
Apr 21 2017 23:42
@xgrommx all those refolds combinations are crazy :)
I had seen elgot and coelgot as a joke in some talk :)
Denis Stoyanov
@xgrommx
Apr 21 2017 23:45
@rickmed I have some experiments with List,Tree,RoseTree,Nat,Church
for example takeWhile or dropWhile paramorphism and catamorphism
Rick Medina
@rickmed
Apr 21 2017 23:55
I think I have seen the tree and rosetree ones! :thumbsup: @xgrommx
Denis Stoyanov
@xgrommx
Apr 21 2017 23:57

@rickmed

data Tree a = Empty | Node (Tree a) a (Tree a) deriving (Functor, Show)

-- data TreeF a b = EmptyF | NodeF b a b deriving (Functor, Show)

newtype TreeC a = TreeC { runTree :: forall r. r -> (r -> a -> r -> r) -> r } deriving Functor

makeBaseFunctor ''Tree

type instance Base (TreeC a) = TreeF a

newtype ListC a = ListC { runList :: forall r. r -> (a -> r -> r) -> r } deriving Functor

type instance Base (ListC a) = ListF a

instance Show a => Show (TreeC a) where
  show m = runTree m "Empty" (\l v r -> "(Node " <> l <> " " <> show v <> " " <> r <> ")")

emptyC :: TreeC a
emptyC = TreeC const 

nodeC :: TreeC a -> a -> TreeC a -> TreeC a
nodeC l v r = TreeC $ \e n -> n (runTree l e n) v (runTree r e n)

unNode :: TreeC a -> Maybe (TreeC a, a, TreeC a)
unNode m = runTree m Nothing $ \l v r -> Just (
  maybe emptyC (\(l1, v1, r1) -> nodeC l1 v1 r1) l, 
  v, 
  maybe emptyC (\(l2, v2, r2) -> nodeC l2 v2 r2) r)

fromTree :: Tree a -> TreeC a
fromTree = cata embed

-- fromTree = ana coalg where
--   coalg :: Coalgebra (TreeF a) (Tree a)
--   coalg = \case
--     Empty -> EmptyF
--     Node l v r -> NodeF l v r

toTree :: TreeC a -> Tree a
toTree = cata embed
-- toTree = cata alg where
--   alg :: Algebra (TreeF a) (Tree a)
--   alg = \case
--     EmptyF -> Empty
--     NodeF l v r -> Node l v r

-- toTree :: TreeC a -> Tree a
-- toTree = ana (maybe EmptyF (\(l, v, r) -> NodeF l v r) . unNode)

instance Recursive (TreeC a) where
  project :: TreeC a -> TreeF a (TreeC a)
  project = maybe EmptyF (\(l, v, r) -> NodeF l v r) . unNode

instance Corecursive (TreeC a) where
  embed :: TreeF a (TreeC a) -> TreeC a
  embed = \case
    EmptyF -> emptyC
    NodeF l v r -> nodeC l v r

Church Tree -> Tree