Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
joschect committed Jul 28, 2020
1 parent 0eaad02 commit eb7182f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
27 changes: 12 additions & 15 deletions src/components/chainable-state/chainable-state.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
import {State} from '../../monads/state';
import { State } from "../../monads/state";
import * as React from "react";

export const Chainable: React.FC<{}> = (props) => {
return (
<>
<h2>ChainableState</h2>
<div>
Chainable state management
</div>
<ChainableState />
<div>Chainable state management</div>
<ChainableState />
</>
);
};

const IncState = (s: number): [number, number] => {
return [s * 2, s+1];
}
return [s * 2, s + 1];
};

const Inc = State(IncState)
const Inc = State(IncState);

const ChainableState: React.FC<{}> = (props) => {
const [myValue, setMyValue] = React.useState(0);
const incrementCallback = React.useCallback(() => {
setMyValue(Inc.runFN(myValue)[1])
setMyValue(Inc.runFN(myValue)[1]);
}, [myValue, setMyValue]);
return <NoState value={myValue} onIncrement={incrementCallback} />;
};

const NoState: React.FC<{value: number, onIncrement: () => void}> = (props)=> {
return <button onClick = {props.onIncrement}>
{props.value}
</button>

}
const NoState: React.FC<{ value: number; onIncrement: () => void }> = (
props
) => {
return <button onClick={props.onIncrement}>{props.value}</button>;
};
18 changes: 9 additions & 9 deletions src/monads/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ export type StateFunction<S, R> = (s: S) => [R, S];

export interface State<S, R> {
bind: <N>(fn: (V: R) => State<S, N>) => State<S, N>;
runFN: (s: S) => [R,S];
runFN: (s: S) => [R, S];
}

class InternalState<S,R> implements State<S,R> {
private _sfn: StateFunction<S,R>;
class InternalState<S, R> implements State<S, R> {
private _sfn: StateFunction<S, R>;
// private _state: S
public constructor(sfn: StateFunction<S,R>) {
public constructor(sfn: StateFunction<S, R>) {
this._sfn = sfn;
}

public bind<N>(fn: (v: R) => State<S,N>): State<S,N> {
return new InternalState((s:S)=> {
public bind<N>(fn: (v: R) => State<S, N>): State<S, N> {
return new InternalState((s: S) => {
const [v, ns] = this.runFN(s);
return fn(v).runFN(ns);
});
Expand All @@ -24,6 +24,6 @@ class InternalState<S,R> implements State<S,R> {
}
}

export function State<S,R>(sfn: StateFunction<S,R>): State<S,R> {
return new InternalState<S,R>(sfn);
}
export function State<S, R>(sfn: StateFunction<S, R>): State<S, R> {
return new InternalState<S, R>(sfn);
}

0 comments on commit eb7182f

Please sign in to comment.