-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strawman proposal for providing Fantasyland Stream type in a separate package #675
Open
semmel
wants to merge
3
commits into
mostjs:master
Choose a base branch
from
semmel:fl-wrapper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
node_modules/ | ||
.git/ | ||
.idea/ | ||
examples/ | ||
benchmark/ | ||
experiments/ | ||
perf/ | ||
test/perf/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"author": "[email protected]", | ||
"description": "Fantasy-Land Api for @most/core", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.es.js", | ||
"require": "./dist/index.js" | ||
} | ||
}, | ||
"files": [ | ||
"type-definitions", | ||
"dist" | ||
], | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"module": "dist/index.es.js", | ||
"name": "@most/core-fl", | ||
"scripts": { | ||
"build:dist": "rollup -c", | ||
"test": "mocha --require ts-node/register ./test/*test**" | ||
}, | ||
"type": "module", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"@most/disposable": "^1.3.0", | ||
"@most/prelude": "^1.8.0", | ||
"@most/scheduler": "^1.3.0", | ||
"@most/types": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"@rollup/plugin-typescript": "^11.1.6", | ||
"@types/ramda": "^0.29.10", | ||
"fp-ts": "^2.16.2", | ||
"mocha": "^10.3.0", | ||
"ramda": "^0.29.1", | ||
"rollup": "^4.10.0", | ||
"rollup-plugin-typescript2": "^0.36.0", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// import typescript from '@rollup/plugin-typescript' | ||
import typescript from 'rollup-plugin-typescript2' | ||
import { nodeResolve } from '@rollup/plugin-node-resolve' | ||
import pkg from './package.json' assert { type: 'json' } | ||
|
||
export default { | ||
input: 'src/index.ts', | ||
plugins: [ | ||
nodeResolve({ | ||
extensions: ['.mjs', '.js', '.json', '.node'] | ||
}), | ||
typescript() | ||
], | ||
external: [ | ||
'@most/scheduler', | ||
'@most/disposable', | ||
'@most/prelude' | ||
], | ||
output: [ | ||
{ | ||
file: pkg.main, | ||
format: 'umd', | ||
name: 'mostCoreFl', | ||
sourcemap: true, | ||
globals: { | ||
'@most/scheduler': 'mostScheduler', | ||
'@most/disposable': 'mostDisposable', | ||
'@most/prelude': 'mostPrelude' | ||
} | ||
}, | ||
{ | ||
file: pkg.module, | ||
format: 'es', | ||
sourcemap: true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Stream, Sink, Scheduler, Disposable } from '@most/types' | ||
import { | ||
continueWith, | ||
empty, | ||
filter, | ||
map, | ||
ap, | ||
now, | ||
chain, | ||
never | ||
} from '../../core' | ||
|
||
interface FunctorFantasyLand<A> { | ||
['fantasy-land/map']<B>(fn: (a: A) => B): FunctorFantasyLand<B> | ||
} | ||
|
||
export const fantasyLand = <A>(stream: Stream<A>): FantasyLandStream<A> => | ||
new FantasyLandStream(stream) | ||
|
||
export class FantasyLandStream<A> implements Stream<A>, FunctorFantasyLand<A> { | ||
constructor(private readonly stream: Stream<A>) {} | ||
|
||
run(sink: Sink<A>, scheduler: Scheduler): Disposable { | ||
return this.stream.run(sink, scheduler) | ||
} | ||
|
||
['fantasy-land/concat'](nextStream: Stream<A>): FantasyLandStream<A> { | ||
return fantasyLand<A>(continueWith(() => nextStream, this.stream)) | ||
} | ||
|
||
['fantasy-land/empty']<B>() { | ||
return fantasyLand<B>(empty()) | ||
} | ||
|
||
['fantasy-land/filter'](predicate: (value: A) => boolean) { | ||
return fantasyLand<A>(filter(predicate, this.stream)) | ||
} | ||
|
||
['fantasy-land/map']<B>(fn: (value: A) => B): FantasyLandStream<B> { | ||
return fantasyLand<B>(map(fn, this.stream)) | ||
} | ||
|
||
['fantasy-land/ap']<B>(mapper: Stream<(value: A) => B>) { | ||
return fantasyLand<B>(ap(mapper, this.stream)) | ||
} | ||
|
||
['fantasy-land/of']<B>(value: B) { | ||
return fantasyLand<B>(now(value)) | ||
} | ||
|
||
['fantasy-land/chain']<B>(fn: (value: A) => Stream<B>) { | ||
return fantasyLand<B>(chain(fn, this.stream)) | ||
} | ||
|
||
['fantasy-land/zero']() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way I read the FL spec, It seems like you'll need to ensure that |
||
return fantasyLand<A>(never()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { fantasyLand, FantasyLandStream } from './FantasyLandStream' | ||
import { compose, curry2 } from '@most/prelude' | ||
import { Stream } from '@most/types' | ||
|
||
import { periodic as _periodic, take as _take, tap as _tap } from '../../core/src' | ||
|
||
export const periodic = compose(fantasyLand, _periodic) | ||
|
||
interface Take { | ||
<A>(n: number, s: Stream<A>): FantasyLandStream<A> | ||
<A>(n: number): (s: Stream<A>) => FantasyLandStream<A> | ||
} | ||
export const take: Take = curry2((x, y) => fantasyLand(_take(x, y))) | ||
|
||
interface Tap { | ||
<A>(f: (a: A) => any, s: Stream<A>): FantasyLandStream<A> | ||
<A>(f: (a: A) => any): (s: Stream<A>) => FantasyLandStream<A> | ||
} | ||
export const tap: Tap = curry2((x, y) => fantasyLand(_tap(x, y))) | ||
|
||
export { fantasyLand, FantasyLandStream } | ||
export { runEffects } from '../../core' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { describe, it } from 'mocha' | ||
import { assert } from '@briancavalier/assert' | ||
import { newDefaultScheduler } from '@most/scheduler' | ||
import { Stream } from '@most/types' | ||
|
||
import { periodic, runEffects, take, tap, FantasyLandStream } from '../src/index' | ||
import { pipe } from 'fp-ts/function' | ||
import { map as mapR } from 'ramda' | ||
|
||
const map = mapR as unknown as <A, B>(fn: (x: A) => B) => (functor: FantasyLandStream<A>) => FantasyLandStream<B> | ||
|
||
const defaultScheduler = newDefaultScheduler() | ||
const runEff = <A>(s: Stream<A>): Promise<void> => runEffects(s, defaultScheduler) | ||
|
||
describe('fantasy-land', function () { | ||
// const x = [0, 1, 2, 3] | ||
// const sampleError = new Error('sample error') | ||
|
||
it('the types should line up', function () { | ||
return runEff(take(2, periodic(10))) | ||
.then(res => { | ||
assert(typeof res === 'undefined') | ||
}) | ||
}) | ||
|
||
it('map', () => pipe( | ||
periodic(10), | ||
take(2), | ||
map(() => 'foo'), | ||
tap(x => { | ||
assert(x === 'foo') | ||
}), | ||
runEff | ||
)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"declarationDir": "./dist", | ||
"target": "ES2020" | ||
}, | ||
"extends": "../../tsconfig", | ||
"include": [ | ||
"src", | ||
"test" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's probably some discussion to be had on what a proper stream monoid should be. There might even be more than 1 valid monoid instance.
The way you've used
continueWith
here is nice. It's like a simpleswitch
. I've come to believe thatmerge
is also a pretty good semigroup.Any thoughts or insights on which might be the better default semigroup instance? Are there others? See my other, related comment about
zero
as well.