These are chat archives for ramda/ramda

25th
May 2017
Mika Kalathil
@MikaAK
May 25 2017 01:57
Is there any way to wrap a function a promise quickly? So if I have a fn called setIsOpen = (open) => this.isOpen = true is there a way to wrap it so it ends up being setIsOpen = (open) => () => this.isOpen = true?
Otto Nascarella
@ottonascarella
May 25 2017 02:49
I gotta say, @Bradcomp.... R.sequence and data.task brought up a very clean workflow over here. Thanks!
TJ Peden
@tjpeden
May 25 2017 03:47
Is there a function that will remap keys of an object to new keys with the same values?
TJ Peden
@tjpeden
May 25 2017 03:50
@lucasschejtman exactly, thanks!
Jason Shin
@JasonShin
May 25 2017 06:01
@JasonShin
guys, in Immutablejs, how do you compose its functions?
I want to use Map.map, Map.toArray, etc using lodash/fp flowRight / ramda.compose
so it would look like
flowRight(
  toArray
  map(myMapFunc)
)(myMap)
Jason Shin
@JasonShin
May 25 2017 06:31
anyone?
=)..
Tom Harding
@i-am-tom
May 25 2017 07:23
Ramda’s pipe function
Joey Figaro
@joeyfigaro
May 25 2017 12:45
@i-am-tom Why pipe over compose?
Tom Harding
@i-am-tom
May 25 2017 12:46
OH, I misread - thought the question was specifically left-to-right composition
Niloy Mondal
@niloy
May 25 2017 12:46
Why not? pipe reads left-> right, top->bottom
Tom Harding
@i-am-tom
May 25 2017 12:47
@JasonShin
flowRight(
  x => x.toArray(),
  x => x.map(myMapFunc)
)(myMap)
Jason Shin
@JasonShin
May 25 2017 13:35
nice
thanks
Will White
@willnwhite
May 25 2017 14:11
Hi. Are there some fun exercises to play around with Ramda?
Matthew Willhite
@miwillhite
May 25 2017 14:12
I like to play “refactor legacy codebase” :sweat_smile:
Will White
@willnwhite
May 25 2017 14:12
lol
Galileo Sanchez
@galileopy
May 25 2017 14:35
I've asked the same question in Bacon.js, but there is almost no activity there, so I'll ask here too, please excuse me those that are in both channels
if I have Stream Stream Array , how do I convert it to Stream Array?
there is no flatten method, and flatMap, flatMapConcat, and others need a second stream
I'd like to flatten the contents of the current stream
the thing is I'm using Bacon.fromPoll(x, () => Bacon.fromPromise(returnPromise()))
flatMap identity perhaps?
David Chambers
@davidchambers
May 25 2017 14:48
R.unnest should do the trick (if Bacon is compatible with old versions of Fantasy Land).
R.unnest :: Chain m => m (m a) -> m a
This is known as join in Haskell (and in Sanctuary).
Denis Stoyanov
@xgrommx
May 25 2017 14:54
@galileopy flatMap(x => x) try it
Just RAG
@justrag
May 25 2017 14:56
Is R.unnest the highest level of R.unny and R.unner? ;)
Galileo Sanchez
@galileopy
May 25 2017 14:57
@xgrommx thanks it worked
Gabe Johnson
@gabejohnson
May 25 2017 15:07
@JasonShin @i-am-tom I think you could use http://ramdajs.com/docs/#invoker as well
flowRight(
  invoker(0, 'toArray'),
  invoker(1, 'map')(myMapFunc)
)(myMap)
arguably less readable for this particular example, but good for defining reusable functions
Brad Compton (he/him)
@Bradcomp
May 25 2017 15:11
@justrag :joy:
Galileo Sanchez
@galileopy
May 25 2017 15:29
how complex could become working with canvas in a functional way? I have a polling function that will update the list of locations that need to be shown periodically
I see 2 approaches, one is to create a marker for every location every time polling has finished
the other is to keep a list of references to existing markers, adding new marker only as needed, and modify their state whenever a new falue is available
a third option would be to create a stream of streams, each updating itself as needed, and the stream of streams will create a new stream every time there is a new marker to keep track of
that third option involves the creation of a new api endpoint that allows me to query each marker individually, which would result in a lot more api calls
Galileo Sanchez
@galileopy
May 25 2017 15:42
it might not involve that, I could simply split the result and do the above
oh R.memoize allows me to just create one
that one is nice
Barton Petersen
@bpetersen
May 25 2017 17:57

TLDR: What is unit testing like with Futures and chaining? Do you have any examples of unit tests covering things like that? Do you even bother with it?

I’m not sure if this is an appropriate place to ask this question, so forgive me if that’s the case. Right now my team is experimenting with utilizing a more functional approach to software development. We all come from a TDD background with all kinds of mock and stubbing experience. We also don’t like the mocks so much :)

Our general architecture is moving toward composition at the outer edges of the system and then pushing branching logic into pure functions (except IO currently). We’re mostly on board with not testing those compose functions at the unit level, but rather just using integration or acceptance tests. But the thing that is causing some worry is general omitted code where we can’t determine correctness through outside behavior alone. Here’s an example:

getData(dataIds) {
  return getDataFromCache(dataIds)
    //.then(filterExpiredData)  //Whoops, we should be calling this.
    .then(getMissingDataFromApi)
}

My question is, will taking the next step into things like futures and chaining them together help our testing situation? Will we be able to test our compositions without mocks? Or would we still not test compositions at the unit level?

Any insight would be greatly appreciated.

Michael Rosata
@mrosata
May 25 2017 17:58
@bpetersen testing compositions is fairly straightforward if you think of the entire composition as a single function
with Futures, (I use mocha), running async tests using the done argument is like testing a promise
I use a utility to put in the rejected callback on the future that will fail the test
import identity from 'ramda/src/identity'
import partial from 'ramda/src/partial'
import compose from 'ramda/src/compose'
import tap from 'ramda/src/tap'

const FAIL_MSG = chalk.bold.red('Test Executed Unreachable Code')

/**
 * Create a function to use as contingency for unreachable code
 * in an asynchronous test.
 *
 * @param doneCallback
 * @return {*}
 */
export default function unreachableTestCode(doneCallback, verbose = 0) {
  return compose(
    partial(
      doneCallback,
      [new Error(FAIL_MSG)]
    ),
    !verbose ? identity : tap(
      console.log.bind(console,
        `\n${ chalk.bold.underline('TEST STDOUT') + chalk.bold('  >>  ') }\n`)
    )
  )
}
That could be overkill, a test would look like this:
  import unreachable from '../helpers/unreachableTestCode'

  it('testing of a future', function (done) {
      someFutureReturingFn.fork(
        unreachable(done/* optional boolean argument to logout error */),
        (resolvedVal) => {
          /* some tests go here */ 
          done()
        }
      )
    })
Michael Rosata
@mrosata
May 25 2017 18:12
@bpetersen as for the mocking, you would only need to test the input/out of a composition. So you shouldn't have to mock anything
Barton Petersen
@bpetersen
May 25 2017 18:20
Thanks for the reply @mrosata. I really like the idea of treating the composition as a single function with inputs and outputs like any other function. Also, the Future test example was very helpful. Question on the point of mocking/stubbing. Does that mean that if one of our functions in the composition did some IO, like fetch from an api for example, we would just go ahead and let it do it? Or is there another way we can verify the correct behavior without going over the wire?
Michael Rosata
@mrosata
May 25 2017 18:38
@bpetersen If your doing I/O in a function, I think you should probably test that function in isolation. I use chai-fetch with API calls, so then I use a composition that contains an API call in a test, but I'm not really testing the composition as much as testing the side-effect, does it call the correct url, does it call it with the correct properties. I'm not a testing guru or anything, but it seems to work and with chai fetch there's no bloat in the code. I'll send you a snippet in a PM
Bijoy Thomas
@bijoythomas
May 25 2017 20:15
Are there any plans to support valueOf like R.toString? For example, R.maxBy(valueOf) .. Or is there already a way to do this?
Joey Figaro
@joeyfigaro
May 25 2017 20:15
Anyone know if there’s a REPL for sublime text 3?
Bijoy Thomas
@bijoythomas
May 25 2017 20:18
@joeyfigaro Not sure about a REPL within sublime, but you can always setup a Build system to run your code
Joey Figaro
@joeyfigaro
May 25 2017 20:19
Using babel and the build process isn’t quick enought to make that super attractive :/
Denis Stoyanov
@xgrommx
May 25 2017 20:20
@joeyfigaro use jsbin :smile: or ramda repl
Bijoy Thomas
@bijoythomas
May 25 2017 20:21
@joeyfigaro was talking about the Sublime build system .. you can set it up to run your code in NodeJS for example
Denis Stoyanov
@xgrommx
May 25 2017 20:21
use vscode
Joey Figaro
@joeyfigaro
May 25 2017 20:22
@xgrommx vscode has a repl? :0
Denis Stoyanov
@xgrommx
May 25 2017 20:23
yes
vscode has terminal
built-in
Bijoy Thomas
@bijoythomas
May 25 2017 20:24
@xgrommx I'm a big fan of the online REPL too .. ramda and ramda-fantasy are available .. its awesome
Denis Stoyanov
@xgrommx
May 25 2017 20:25
@bijoythomas probably u will be fan of https://github.com/princejwesley/Mancy
Otto Nascarella
@ottonascarella
May 25 2017 20:25
Mancy is super cool.
Denis Stoyanov
@xgrommx
May 25 2017 20:25
yes
I need support for elm,purescript or haskell :smile:
but I have HFM
Bijoy Thomas
@bijoythomas
May 25 2017 20:29
@xgrommx thanks! It does look pretty kool
Denis Stoyanov
@xgrommx
May 25 2017 20:31
@bijoythomas yeah) nice tool
Bijoy Thomas
@bijoythomas
May 25 2017 20:36
My earlier post might have gotten lost so asking again
Are there any plans to support valueOf like R.toString? For example, R.maxBy(valueOf) .. Or is there already a way to do this?
Brad Compton (he/him)
@Bradcomp
May 25 2017 20:42
You could use R.invoker if you wanted to. maxBy(invoker(0, 'valueOf'))
Bijoy Thomas
@bijoythomas
May 25 2017 20:44
@Bradcomp thanks. Never used it before but now see how it can be handy
Brad Compton (he/him)
@Bradcomp
May 25 2017 20:45
Yeah. Another option is a simple lambda: maxBy(x => x.valueOf())
Bijoy Thomas
@bijoythomas
May 25 2017 20:47
:thumbsup:
Joey Figaro
@joeyfigaro
May 25 2017 20:48
@xgrommx Anything you’ve run across for using es6/7 in vscode’s integrated terminal?
Denis Stoyanov
@xgrommx
May 25 2017 20:49
@joeyfigaro if I need dev mode, I use babel-node (you can install it via npm i -g babel-cli)
Joey Figaro
@joeyfigaro
May 25 2017 20:50
Oh, you’re running one module/file at a time through babel-node to evaluate?
Denis Stoyanov
@xgrommx
May 25 2017 20:50
for dev mode - yes
Joey Figaro
@joeyfigaro
May 25 2017 20:51
Ahhh, word. I’ve got my terminal already - looking for breakpoints or inline evaluation (or as close to as possible)
Lucas Schejtman
@lucasschejtman
May 25 2017 22:26
I like Mancy but for vscode users I've found quokkajs really nice as well
Joey Figaro
@joeyfigaro
May 25 2017 23:38
Thanks @lucasschejtman - checking that out now. :)
Joey Figaro
@joeyfigaro
May 25 2017 23:58
@lucasschejtman Quokka is fantastic. Thanks!