Questions tagged [sanctuary]

Sanctuary is a JavaScript functional programming library inspired by Haskell and PureScript.

Sanctuary is a JavaScript functional programming library inspired by Haskell and PureScript. It's stricter than Ramda, and provides a similar suite of functions.

Sanctuary promotes programs composed of simple, pure functions. Such programs are easier to comprehend, test, and maintain – they are also a pleasure to write.

Sanctuary provides two data types, Maybe and Either, both of which are compatible with Fantasy Land. Thanks to these data types even Sanctuary functions which may fail, such as head, are composable.

Sanctuary makes it possible to write safe code without null checks. In JavaScript it's trivial to introduce a possible run-time type error:

words[0].toUpperCase()

If words is [] we'll get a familiar error at run-time:

TypeError: Cannot read property 'toUpperCase' of undefined

Sanctuary gives us a fighting chance of avoiding such errors. We might write:

S.map(S.toUpper, S.head(words))

Sanctuary is designed to work in Node.js and in ES5-compatible browsers.

33 questions
1
vote
0 answers

Sanctuary `Maybe` refers to a value, but being used as type here

I have a function like this export const createMemberId: (x: any) => Maybe = (x: any) => { try { return Just({ memberId: mongoose.Types.ObjectId(x), }); } catch (e) { return Nothing; } }; It…
mohsen saremi
  • 685
  • 8
  • 22
1
vote
1 answer

A more idiomatic way to write sanctuary pipe

I wrote a code snippet that extracts the source object keys (which are supposed to be unknown so I've added a non-existing key baz to be extracted. Every element in the array that I extract - I want to add the key from which It was extracted and…
MaxG
  • 1,079
  • 2
  • 13
  • 26
1
vote
1 answer

Execute Fluture task in middle of Sanctuary pipe

I have a pipe like this: S.pipe([ getRequestFile, // not async S.chain(saveTemporary), // not async S.chain(copyImageToPublicPath), // async S.chain(copyFileToPath), // async …
1
vote
0 answers

Know if value is defined as Nullable or NotNullable when resolving custom scalar type

I'm creating custom scalar types that override the built in Int and Float. I'd like to know whether the schema defines the value as nullable or not. I'd like to resolve an Int differently than Int! in my custom scalar type What I want to do is…
1
vote
2 answers

Merge multiple objects with Sanctuary.js

I am trying to merge multiple objects with Sanctuary. With Ramda.js I would do something like this (see the REPL here): const R = require('ramda'); const initialEntry = { a: 0, b: 1 }; const entries = [{c: 2}, {d: 3, e: 4}, {f: 5, g: 6, h:…
jackdbd
  • 4,583
  • 3
  • 26
  • 36
1
vote
1 answer

Error with simply Maybe funtion

Background I am trying to help someone in StackOverflow with a question regarding Futures and Maybes and Eithers. My first approach is to have a simple function that takes in a Maybe and computes something. Code I am using Sanctuary, but this is…
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
1
vote
2 answers

How to stub Fluture?

Background I am trying to convert a code snippet from good old Promises into something using Flutures and Sanctuary: https://codesandbox.io/embed/q3z3p17rpj?codemirror=1 Problem Now, usually, using Promises, I can uses a library like sinonjs to stub…
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
1
vote
1 answer

Can I make "over" via object propery with Sanctuary

Ramda was my first library of functional programming and now I compare Sanctuary with Ramda. Maybe some my questions are too stupid, but I did not find best way to learn Sanctuary. My question is follow: How can I map array in nested property of…
1
vote
1 answer

How does Curry differs between Sanctuary and Ramda?

I am stuck with the curry examples in "Professor's Frisby..." when using Sanctuary instead of Ramda. I get error: "‘curry2’ expected at most three arguments but received five arguments." while with Ramda works fine. I am sure I am doing something…
aym
  • 233
  • 1
  • 4
  • 17
0
votes
1 answer

fantasy-land confusion on ap method signature

In fantasy-land spec, the signature for ap method is defined as fantasy-land/ap :: Apply f => f a ~> f (a -> b) -> f b This translates as: The container f with value a has a method ap which takes a parameter container f with value of a function (a…
0
votes
1 answer

execute Fluture task with Sancuary Either

I have a pipe like this const asyncFn = (x) => { return Future.tryP(() => Promise.resolve(x + ' str2')) }; const pipeResult = S.pipe([ x => S.Right(x + " str1"), // some validation function …
0
votes
2 answers

Multiple if's refactoring

I have this function, with two ifs where I want to find the user depending on which alphanumeric code I receive. How can I refactor this one with sanctuary-js? //const code = '0011223344'; const code = 'aabbc'; const isNumberCode = code =>…
Ivan
  • 2,463
  • 1
  • 20
  • 28
0
votes
1 answer

Set property when value is Just

I would like to set the property of an object when the value is Just, and not to set the property when the value is Nothing. However, if the value is Nothing, the returned object become Nothing. let person = {name: 'Franz'} const address =…
Franz Wong
  • 1,024
  • 1
  • 10
  • 32
0
votes
1 answer

What does it actually mean Left and Right in sanctuary

> S.reduce(S.flip(S.K),[],S.Left([1,2])) [] > S.reduce(S.flip(S.K),[],S.Right([1,2])) [ 1, 2 ] I was trying to understand sanctuary and its working can anybody explain the above outcomes in detail In my understanding S.reduce takes a mapping…
codefreaK
  • 3,584
  • 5
  • 34
  • 65
0
votes
1 answer

Mapping with sanctuary over fantasy-land IO

I have a small code sample that works as I expected it to when using ramda, but my attempt to port this to sanctuary has failed. const IO = require('fantasy-io'); const S = require('sanctuary'); const R = require('ramda'); const url =…