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
10
votes
1 answer

Custom equality semantics for Immutable.js data structures

I would like Sanctuary to provide Fantasy Land -compatible Map and Set types with value-based equality semantics. Ideally these values would be immutable, though this is not critical since Sanctuary would provide pure functions for merging and…
davidchambers
  • 23,918
  • 16
  • 76
  • 105
6
votes
2 answers

Using `.of` Constructor on Sanctuary Maybe

I'm working through a tutorial on functional programming that shows the following code example using the sanctuary.js library: var S = require('sanctuary') var Maybe = S.Maybe S.add( Maybe.of(3) ,Maybe.of(5) ) .map(n => n * n) I get the error…
4
votes
2 answers

How do I collapse Maybe monads in sanctuary js

Here is a simple chained expression using modern javascript to find the value for a specific key located in a string containing a comma separated list of key-value pairs separated by =. This falls down if the source is null or the key is not found,…
joshperry
  • 41,167
  • 16
  • 88
  • 103
4
votes
4 answers

What's the cleanest fp way to get a property pointed by another property

Given an object that may be null and may have the following properties: { templateId: "template1", templates: { template1: "hello" } } How would you get the template in a failsafe way? (templateId might not be defined, or the…
Tiago Coelho
  • 5,023
  • 10
  • 17
3
votes
1 answer

Picking fields from object with certain RecordType with Sanctuary

I have an object with options that corresponds to the following record type: const AwsRegionsEnum = $.EnumType( 'AWS/Regions', 'http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html', [ 'us-east-1', …
3
votes
2 answers

How should I map over Maybe List?

I came away from Professor Frisby's Mostly Adequate Guide to Functional Programming with what seems to be a misconception about Maybe. I believe: map(add1, Just [1, 2, 3]) // => Just [2, 3, 4] My feeling coming away from the aforementioned guide is…
Ziggy
  • 21,845
  • 28
  • 75
  • 104
2
votes
1 answer

Sanctuary js execute 2 steps of pipe with condition

I have a S.pipe for upload and manipulate incoming request file S.pipe([ getRequestFile, S.chain(saveTemporary), S.chain(checkIfIsImage), S.chain(addWatermarkToImage), // only execute on image S.chain(copyImageToPublicPath), //…
mohsen saremi
  • 685
  • 8
  • 22
2
votes
1 answer

How to log properly in Sanctuary / Fluture?

Background I have a function, called logInfoAsync. Let's consider this function sends some information to a log server over the network. For the purposes of this question let's assume the function is implemented as follows: const logInfoAsync =…
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
2
votes
1 answer

Sanctuary.Js Type Error with Identity Functor

I am playing around following Bartosz Milewski category theory lessons on youtube. He describes Const and Identity functors as the "base" functors can be derived from (probably a liberal paraphrasing on my part). My problem, having implemented…
akaphenom
  • 6,728
  • 10
  • 59
  • 109
2
votes
2 answers

Confusion in understanding Substitution / `ap` type signature and different implementations (functional programming)

I am a student of functional programming, sorry if my question sounds weird--I am trying to wrap my mind around the given type signatures for functions and how they are implemented. Looking at the signature for ap…
1
vote
0 answers

Which JavaScript lib has feature similar to Haskell's do-notation or Scala's for-comprehension?

I am exploring a few JavaScript libraries for functional programming such as Ramda, Sanctuary, etc.,. I've also checked their pipe and compose functions for chaining and composition. But they can't do something like below which use both x and y to…
Pyi Soe
  • 11
  • 1
1
vote
0 answers

Functional programming: handling null results with sanctuary.js and Monad

I am new to the world of functional programming and am trying to work through a few points of confusion to me with Sanctuary.js. Here is the scenario, I would like to use a pipe to handle reading from a mongoose database, checking to make sure I…
user1790300
  • 2,143
  • 10
  • 54
  • 123
1
vote
1 answer

Wrong answer from S.min when strings used

S.min ('1') ('02') =>'02' Why is this even possible? (yes type coercion... but this is Sanctuary) Can Sanctuary be configured so that Nothing is returned when Strings are used? Is there an elegant way to deal with this?
Niel Ryan
  • 1,123
  • 2
  • 7
  • 9
1
vote
1 answer

Data modelling challenges in sanctuary.js

I am building an app based on domain-driven design using functional programming in javascript. I've settled on the sanctuary.js ecosystem as my tool of choice, but I'm facing some challenges in modelling types. To put things in context, let's take…
ceblay
  • 23
  • 4
1
vote
1 answer

Operating on two Eithers

Suppose that you have the following code: import R from "ramda"; import S from "sanctuary"; import { Left, Right } from "sanctuary-either"; const add = R.curry((p1, p2) => p1 + p2); const addOne = add(1); const func1 = () => Right(2); const func2…
antoniom
  • 3,143
  • 1
  • 37
  • 53
1
2 3