## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
Mika Kalathil
@MikaAK
Oooh perfect!
Well I'm hanging around here more for sure, I learned a bunch already thanks guys
David Chambers
@davidchambers
You're welcome. :)
:+1:
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) => {
const thunk =
S.maybe(() => R.append(repl, xs),
idx => () => R.update(idx, repl, xs),
findIndex_(pred, xs));
return thunk();
};

[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
:+1: on the lazy eval of append
David Chambers
@davidchambers
:)
The implementation is a bit less elegant, but this may be a price worth paying.
Julien Goux
@jgoux
Hi
Is there a global way of disabling sanctuary's typecheck without doing the const {create, env} = require('sanctuary') part ?
I can always import sanctuary and reexport it and then use my own "S" alias, but I'd prefer to use it without that
What is the point of having the typecheck with NODE_ENV=production ?
Most library disable warnings/check/errors on this flag (thinking about react)
Rick Medina
@rickmed
hey folks, anyone have references to good/balanced articles about the benefits/drawbacks of fp?
David Chambers
@davidchambers
I do not, @rickmed, but I would like to read some. :)
Irakli Safareli
@safareli
some time ago I was collecting good articles here but then I stoped and instead i'm just tweeting :p
David Chambers
@davidchambers
This question is well worth discussing, @jgoux. One thing to keep in mind is that type checking can be used to enforce invariants in one's programs which are particularly important in production. Type checking applications of withdraw :: PositiveFiniteNumber -> BankAccount -> BankAccount may not be important during development, but would be crucial in production to prevent negative withdrawals (i.e. deposits).
Rick Medina
@rickmed
yeah the thing is that is relatively easy to find fp introductory stuff or "OOP is bad! use fp instead!" but not that easy too find stuff like "hey, fp is awesome, but here is where is not that awesome..."
Rick Medina
@rickmed
hopefully "but here is where is not that awesome..." was not blasphemy in this channel :laughing:
David Chambers
@davidchambers
Not at all, @rickmed. ;)
If type checking is being used in this way, one would not want it to be disabled simply because NODE_ENV=production. Another situation in which tying checkTypes to NODE_ENV is problematic is property-based testing. These tests can be slow, so one may wish to disable type checking when running such tests locally. One would not want to set NODE_ENV=production, though, as this may have other effects such as having the test runner connect to a live database. The general problem is that having different behaviours depend on a single value means one can't adjust one setting independently of the others. A better solution would be to have a dedicated environment variable such as SANCTUARY_CHECK_TYPES. In fact, there's a module named plaid-sanctuary in Plaid's private npm registry which simply creates and exports a Sanctuary module with the value of checkTypes determined by this environment variable.
David Chambers
@davidchambers
In general, though, I don't think libraries should dictate a certain configuration mechanism. Environment variables aren't a good solution for front-end projects, and even for back-end projects some teams prefer config files. The easy option would be to ask users to mutate the module via S.checkTypes = false, but this would prevent type checking from being used in one module but not another, and would cause problems for projects which depend on Sanctuary both directly and indirectly.
The current approach is certainly cumbersome, but I believe it to be the best approach of the one's I've considered. The downsides of relying on NODE_ENV outweigh the benefits, but it's possible there's an equally convenient solution with less significant drawbacks.
Mika Kalathil
@MikaAK
if you happen to be using webpack you can alias you’re file to rexport S as sanctuary so it still reads as import * as S from ‘sanctuary’
David Chambers
@davidchambers
I have an apartment to call "home" for the first time in three months. I'm about to put my head down and spend the weekend tackling the tasks in #255. I'll start with Murmur and go from there. :headphones:
David Chambers
@davidchambers
Stefano Vozza
@svozza
Nice!
David Chambers
@davidchambers
David Chambers
@davidchambers
I've opened a pull request to define Z.lift2 and Z.lift3: sanctuary-js/sanctuary-type-classes#5.
Hans Krutzer
@hkrutzer
is there an equivalent to data.maybe .orElse?
ven
@vendethiel
Hans Krutzer
@hkrutzer
thanks!
David Chambers
@davidchambers
Your work on sanctuary-set is extremely exciting to see, @scott-christopher! I love the ASCII diagrams. You're welcome to open a pull request at any point. It might be useful for discussion purposes even if you feel there's still a lot of work to be done.
Scott Christopher
@scott-christopher
sanctuary-js/sanctuary-set#1
:)
David Chambers
@davidchambers
Barry G
@bgits
I'm still new to Maybe and Just types, but I'm wondering if some of this is just to make a small cohort of people who are used to functional languages that don't make use of closures feel at home using the same style in JS? If I'm doing a null check or going into a container to check, feel similar in terms of work and understanding.
Keith Alexander
@kwijibo
@bgvianyc null checks don't compose, Maybes do
My background is definitely not in languages like Haskell, I've moved from more traditional JS styles towards this sort of thing. There are definite benefits to the container types beyond just null checks. For me, being able to write functions that take normal types and assume they aren't null is a really nice way to prevent bugs. The containers (like Maybe, Either, and Future) encode information about your program into the structure, so you can know more about your values by looking at the code, and so you can write code that only deals with the specific logic it handles, while letting the containers handle a lot of the heavy lifting of validation, async vs sync, null checks, etc.
Keith Alexander
@kwijibo
so if you break your program down into a series of functions that transform your data R.pipe(f, g, h, i), and f might return a null/undefined, then you need to do either do null checks on g, h,and i as well (even though they are safe themselves), or you need to nest everything inside the first null check (losing compositionality)
Barry G
@bgits
These things sounds good, would be nice to see a nice write up /video with examples to really understand.
The other question for folks using this style in production, is how do your coworkers respond to reading code/ working on a codebase written in this style?
Personally, that's a tough one. I've introduced a number of FP concepts at my work, but container types aren't one of them (other than the occasional Future). While I see places where they could be used to good effect, there is definitely a barrier there.
Keith Alexander
@kwijibo
I really like that blogpost
Barry G
@bgits
no, but will take a look
Keith Alexander
@kwijibo
re how do colleagues respond: I suppose it depends on the colleagues in question, and how it's introduced :) Things like Maybe and Either are not very common in javascript of course, but on the other hand, the basic pattern should be sort of familiar from working with Arrays and Promises
Keith Alexander
@kwijibo
eg: [].map(f)has the same effect as S.Nothing().map(f)
and [3].map(f) has the same effect as S.Just(3).map(f)