Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Irakli Safareli
@safareli

Then if we use this f and concat some other (Applicative m, Monoid m) => m to it than the result should be this other thing

f(0).concat(Identity([])) // Identity([])

empty represents empty for any monoid

David Chambers
@davidchambers
Oh, I see now.
If we have the special empty value and we want to get an actual empty value, how would we do so? How would we get from empty to '', for example?
Irakli Safareli
@safareli
empty.concat(String.empty)
and that concat on empty is just id so it returns String.empty
David Chambers
@davidchambers
That makes sense.
Irakli Safareli
@safareli
:tada:
David Chambers
@davidchambers
Very cool stuff, @safareli!
Irakli Safareli
@safareli
yeh I'm excited! This weekend I would set up a repo for that, but first i should thinking on the name :smile:
David Chambers
@davidchambers
That's often the trickiest part.
Irakli Safareli
@safareli
here is wip implementation of that empty|of safareli/quasi#1
Wellington Mendoza
@well1791
Hi there!
David Chambers
@davidchambers
Hello!
Wellington Mendoza
@well1791
hey @davidchambers !! I'm here just to learn about great minds!! but also I want to do a presentation for my teammates to use sanctuary and.. If it's ok I would like to ask some questions! being this the first one: what are the major benefits of using sanctuary instead of something like purescript/elm or alikes??
the first thing I'll say is that when you use something like purescript it's a bit hard to mix some libraries..
but still not sure.. I haven't played with purescript or elm before.. so that's why I'm asking here.. ^^
David Chambers
@davidchambers
If using PureScript or Elm is an option, that's the way to go. Sanctuary may be useful for existing JavaScript projects, though, and for other situations where using a "new" language is not an option for some reason or another.
Brad Compton (he/him)
@Bradcomp
For me one benefit is that Sanctuary is still JavaScript. This means you have better interop with the rest of the JS community, and less buy in from the rest of the team.
:point_up:
It's often not reasonable to switch to a pure FP language for one reason or another, as much as we would like to.
David Chambers
@davidchambers
Well put, @Bradcomp. Another way to think about Sanctuary is as a stepping stone. One isn't going to go from writing imperative JavaScript to writing Haskell overnight. Underscore, Ramda, and Sanctuary make such a transition possible over months or years.
Wellington Mendoza
@well1791
@davidchambers @Bradcomp thanks for your responses!! actually that's a good reason to choose sanctuary! thanks for your time guys! you're awesome!
David Chambers
@davidchambers
You're welcome. :)
Mika Kalathil
@MikaAK
Hey guys quick couple questions for you, how does one extract a value from a Maybe? If I'm doing Maybe.of(2).map(someThing) do i just do .value to get the value? Is there some way to chain a fromMaybe to a Maybe chain?
David Chambers
@davidchambers
It's nice to use pipe rather than method chaining, @MikaAK. Let me find an example featuring fromMaybe.
Mika Kalathil
@MikaAK
It's hard to use pipe in my situation maybe you can provide some guidance @davidchambers? I wasn't sure how to use it
const replaceIndex = curry((array, replacement, index) => set(lensIndex(index), replacement, array))

const replaceInArray = (findFn, array, replacement) => findIndex(findFn)
  .map(replaceIndex(array, replacement))
  // need fromMaybe(array.concat(replacement))
In general I have a hard time with things like this because I'm unsure of how to compose them together point free style when I have a requirement on args like that
Irakli Safareli
@safareli
if findIndex could return Maybe Index then you replaceIndex(array, replacement) would be called with Index only if it is present, if findIndex returns Nothing then map will just ignore that function (I might not get you question thou)
David Chambers
@davidchambers
What is the type of replaceIndex, @MikaAK?
Mika Kalathil
@MikaAK
@safareli correct, but how to apply fromMaybe(array.concat(replacement)) to replaceInArray
@davidchambers I'm actually not to great yet with signatures :( I have a hard time with creating them but I think it would be (a, b, c) -> a
David Chambers
@davidchambers
Let me see whether I understand the problem you're attempting to solve. Do you want to transform something like ['foo', 'bar', 'baz'] into ['foo', 'bar', 'quux'] by replacing the first occurrence of 'baz' with 'quux'?
Mika Kalathil
@MikaAK
basically, say I had ['foo', 'bar', 'baz'] I would like to use findAndReplace(R.equals('bar'), 'quux') to replace to ['foo', 'quux', 'baz'] or if the source array was ['foo', 'baz'] it would be ['foo', 'baz',' quux']
David Chambers
@davidchambers
I'm confused by the last bit. Do you want ['foo', 'baz'] to produce ['foo', 'baz', 'quux']?
Oh, I think I see what you're saying now. Replace the first occurrence if found, otherwise append to the input.
Mika Kalathil
@MikaAK
Exactly! Sorry if my wording was confusing, function should of been named addOrReplace
David Chambers
@davidchambers
I'm with you now. Give me a few minutes to play with it in the REPL. :)
Mika Kalathil
@MikaAK
Thanks :D
David Chambers
@davidchambers
@ram-bot
//    findIndex_ :: (a -> Boolean, Array a) -> Maybe Integer
const findIndex_ = (pred, xs) => {
  const idx = R.findIndex(pred, xs);
  return idx >= 0 ? S.Just(idx) : S.Nothing();
};

//    f :: (a -> Boolean, a, Array a) -> Array a
const f = (pred, repl, xs) =>
  S.maybe(R.append(repl, xs),
          S.compose(R.set(R.__, repl, xs), R.lensIndex),
          findIndex_(pred, xs));

[f(R.equals('baz'), 'quux', []),
 f(R.equals('baz'), 'quux', ['foo']),
 f(R.equals('baz'), 'quux', ['foo', 'bar']),
 f(R.equals('baz'), 'quux', ['foo', 'bar', 'baz'])];
ram-bot
@ram-bot
[ [ 'quux' ],
  [ 'foo', 'quux' ],
  [ 'foo', 'bar', 'quux' ],
  [ 'foo', 'bar', 'quux' ] ]
David Chambers
@davidchambers
This is idiomatic, I would say. The only problem is that R.append(repl, xs) happens regardless of whether the resulting value is needed. This inefficiency may or may not bother you. It's worth noting that this is not a problem in Haskell due to lazy evaluation.
Mika Kalathil
@MikaAK
That's actually kinda what I had but I have more steps in between maybe
But S.maybe is what I needed I think
Brad Compton (he/him)
@Bradcomp
Just a note that Ramda has R.update which can be used instead of a lens
R.update
David Chambers
@davidchambers
Good point, @Bradcomp. :)
Brad Compton (he/him)
@Bradcomp
@ram-bot
//    findIndex_ :: (a -> Boolean, Array a) -> Maybe Integer
const findIndex_ = (pred, xs) => {
  const idx = R.findIndex(pred, xs);
  return idx >= 0 ? S.Just(idx) : S.Nothing();
};

//    f :: (a -> Boolean, a, Array a) -> Array a
const f = (pred, repl, xs) =>
  S.maybe(R.append(repl, xs),
          R.update(R.__, repl, xs),
          findIndex_(pred, xs));

[f(R.equals('baz'), 'quux', []),
 f(R.equals('baz'), 'quux', ['foo']),
 f(R.equals('baz'), 'quux', ['foo', 'bar']),
 f(R.equals('baz'), 'quux', ['foo', 'bar', 'baz'])];
ram-bot
@ram-bot
[ [ 'quux' ],
  [ 'foo', 'quux' ],
  [ 'foo', 'bar', 'quux' ],
  [ 'foo', 'bar', 'quux' ] ]
Mika Kalathil
@MikaAK
Oooh perfect!