alexgalays / spacelift Goto Github PK
View Code? Open in Web Editor NEWFunctional utils for Arrays, Objects, Maps, Sets.
License: MIT License
Functional utils for Arrays, Objects, Maps, Sets.
License: MIT License
I saw this here https://github.com/Threestup/monads
Looks like a neat approach for pseudo pattern matching.
let result = Some("Sam")
let message = result.match({
Some: res => `Hello ${res}`,
None : () => 'Hello World',
})
It would be great to have this. In the example above I made the None
case be a function, because you might have an expensive computation there or side effects.
Would be nice to get an NPM package along with types for this library.
Follow up issue on #18
Version 1.x.x won't have
Option
(norResult
) as [...] the addition of?.
in the language, I feel like it's no longer offsetting the choice burden it adds on top ofT | undefined
or?: T
.
I disagree with this conclusion. Option
is both more explicit and more expressive than the undefined
syntax. Option
allows you to compose functions in ways that undefined
just does not have a comparable API for.
Compare (undefined
version)
function go(x: string) {
const a = mayFailA(x);
if (a !== undefined) {
const b = mayFailB(a);
if (b !== undefined) {
const c = mayFailC(b);
if (c !== undefined) return c;
}
}
return attemptTwo(x);
}
const result = go('x')
if(result !== undefined) console.log(result);
with (Option
version), same logic
mayFailA('x')
.flatMap(mayFailB)
.flatMap(mayFailC)
.orElse(() => attemptTwo('x'))
.forEach(console.log);
This is currently my go-to lightweight library for Option
and Result
. I argue if those fundamental monads should be removed, it should be published under a new name, rather than as an "upgrade".
> tuple(1, 2)
=> 1
expected [1, 2]
, but got 1
> tuple("first", "second", "third")
=> "first"
expected ["first", "second", "third"]
, but got "first"
I think this code
export function tuple(arr: any[]): any {
return arr
}
... just needs to be changed to
export function tuple(...arr: any[]): any {
return arr
}
Hey!
Thanks for your work!
I came back to space-lift
after some time and I cannot really run it. I immediately get errors when trying to run examples.
index.ts(10,4): error TS2339: Property 'findIndex' does not exist on type 'ArrayOps<{ id: number; name: string; }>'.
index.ts(11,30): error TS2339: Property 'updateAt' does not exist on type 'ArrayOps<{ id: number; name: string; }>'.
You can also see it some online coding environments. https://codesandbox.io/s/4xn0zj3o40
space_lift_1.default(...).findIndex is not a function
import lift, { update } from 'space-lift'
const people = [
{ id: 1, name: 'jon' },
{ id: 2, name: 'sarah' },
{ id: 3, name: 'nina' }
]
const updatedPeople = lift(people)
.findIndex(p => p.id === 2)
.map(index => lift(people).updateAt(index, person => update(person, { name: 'Nick' })))
.getOrElse(people)
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"tsc": "tsc"
},
"author": "",
"license": "ISC",
"dependencies": {
"space-lift": "^0.6.5",
"typescript": "^2.7.2"
}
}
I'm trying to use the lib in my angular/typescript project, but unfortunately I can't compile it because of this error. I'm using angular-cli 1.7.3, where I configured it's "scripts" section like this:
"scripts": [
"../node_modules/space-lift/es/all.js",
"../node_modules/lodash/core.min.js"
],
And here is my tsconfig
:
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}
The error itself is here:
Any ideas, what I'm doing wrong?
Nice lib!
When working with function than return Option or Result it is very useful to convert one to the other. In that way is possible to chain these e.g.
Some(34)
.toResult("No number")
.flatMap(n => Ok(x * 2))
See
http://package.elm-lang.org/packages/elm-lang/core/latest/Result#fromMaybe
https://doc.rust-lang.org/core/option/enum.Option.html#method.ok_or
Result.toMaybe
would also be great.
https://doc.rust-lang.org/core/result/enum.Result.html#method.ok
Please consider adding these.
Instead of vendoring Mocha's types, would you be up for a PR that pulled them in using @types/mocha
in the package.json? Happy to do that legwork myself, probably mid-to-late next week.
doesn't compile:
error TS2430: Interface 'None' incorrectly extends interface 'Option<never>'.
Types of property 'get' are incompatible.
Type '() => undefined' is not assignable to type '() => never'.
Type 'undefined' is not assignable to type 'never'.
Posting this here mainly for others. I spent considerable time trying to make my import
statement work without success.
Turns out that the latest
tag on npm
is pointing to 1.0.0-beta.3
. This version does not contain [all] the classes of the 0.8.x
version listed in the README. So if something is missing, downgrade to 0.8.7
and you'll be fine.
Would be good to fix the latest
tag on npm until the 1.x series actually has a release.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.