Skip to content
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

Add do notation for Array #2678

Merged
merged 13 commits into from
May 3, 2024
Merged

Conversation

KhraksMamtsov
Copy link
Contributor

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

Do notation (pipe version) for Array module.

Related

  • Related Issue #
  • Closes #

Copy link

changeset-bot bot commented May 1, 2024

🦋 Changeset detected

Latest commit: 0f50aa2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 24 packages
Name Type
effect Minor
@effect/cli Major
@effect/experimental Major
@effect/opentelemetry Major
@effect/platform-browser Major
@effect/platform-bun Major
@effect/platform-node-shared Major
@effect/platform-node Major
@effect/platform Major
@effect/printer-ansi Major
@effect/printer Major
@effect/rpc-http Major
@effect/rpc Major
@effect/schema Major
@effect/sql-mssql Major
@effect/sql-mysql2 Major
@effect/sql-pg Major
@effect/sql-sqlite-bun Major
@effect/sql-sqlite-node Major
@effect/sql-sqlite-react-native Major
@effect/sql-sqlite-wasm Major
@effect/sql Major
@effect/typeclass Major
@effect/vitest Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@mikearnaldi mikearnaldi requested a review from gcanti May 1, 2024 21:12
Copy link
Member

@datner datner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine, but I must know:
why? What can this be used for? 🤔

Also please add more jsdocs, preferably with example 🙏🏻

packages/effect/src/Array.ts Outdated Show resolved Hide resolved
@mikearnaldi
Copy link
Member

This looks fine, but I must know: why? What can this be used for? 🤔

Also please add more jsdocs, preferably with example 🙏🏻

Do in array is as important as do in stream it's used to build up on products basically (zipWith on steroids)

@KhraksMamtsov
Copy link
Contributor Author

This looks fine, but I must know: why? What can this be used for? 🤔

Also please add more jsdocs, preferably with example 🙏🏻

I use this to generate independent props for stories in storybook

const btnProps = pipe(
    RA.Do,
    RA.bind("size", () => ["small", "medium", "large"] as const),
    RA.bind("theme", () => ["dark", "light", "contrast-light", "contrast-dark"] as const),
    RA.bind("disabled", () => [false, true]),
    RA.bind("loading", () => [false, true]),
    RA.bind("leftIcon", () => [null, "arrow-left", "disk"]),
    RA.bind("rightIcon", () => [null, "arrow-right", "save"]),
    RA.bind("text", () => [lorem(1), lorem(3), lorem(10)])
)

@gcanti
Copy link
Contributor

gcanti commented May 2, 2024

can be used to simulate "array comprehension"

Array comprehension is a technique in programming that allows you to create new arrays by iterating over existing ones and applying specific conditions or transformations to the elements. It's like assembling a new collection from pieces of other collections based on certain rules.

Haskell

[(x,y) | x <- [1,3,5], y <- [2,4,6], x < y]
-- Output: [(1,2),(1,4),(1,6),(3,4),(3,6),(5,6)]

Effect

import { Array as Arr, pipe } from "effect"

const result = pipe(
  Arr.Do,
  Arr.bind("x", () => [1, 3, 5]),
  Arr.bind("y", () => [2, 4, 6]),
  Arr.filter(({ x, y }) => x < y), // condition
  Arr.map(({ x, y }) => [x, y] as const) // transformation
)

console.log(result)
/*
Output:
[ [ 1, 2 ], [ 1, 4 ], [ 1, 6 ], [ 3, 4 ], [ 3, 6 ], [ 5, 6 ] ]
*/

We could use this short explanation / example (without the Haskell bit) in the docs

@KhraksMamtsov I've merged this PR that makes it easier to define the do notation for Array #2681

@KhraksMamtsov
Copy link
Contributor Author

@gcanti If you don't mind, I'll add

// doNotation.ts
export type EmptyScope<F extends TypeLambda> = Kind<F, never, never, never, {}>
// module
export const Do: doNotation.EmptyScope<ModuleTypeLambda> = ...

@gcanti
Copy link
Contributor

gcanti commented May 2, 2024

I think our convention is not to export anything public from the internal folder

@datner
Copy link
Member

datner commented May 2, 2024

Do in array is as important as do in stream it's used to build up on products basically (zipWith on steroids)

I conceptualize this well 🤔

but the size doesn't explode?

const btnProps = pipe(
    RA.Do,
    RA.bind("size", () => ["small", "medium", "large"] as const),
    RA.bind("theme", () => ["dark", "light", "contrast-light", "contrast-dark"] as const),
    RA.bind("disabled", () => [false, true]),
    RA.bind("loading", () => [false, true]),
    RA.bind("leftIcon", () => [null, "arrow-left", "disk"]),
    RA.bind("rightIcon", () => [null, "arrow-right", "save"]),
    RA.bind("text", () => [lorem(1), lorem(3), lorem(10)])
)

thats an array of 1,296 members, no?

This is not to say not to add this, I'm purely interested in the concept and utility

packages/effect/src/Array.ts Outdated Show resolved Hide resolved
packages/effect/src/Array.ts Outdated Show resolved Hide resolved
packages/effect/src/Array.ts Outdated Show resolved Hide resolved
packages/effect/src/Array.ts Outdated Show resolved Hide resolved
packages/effect/src/internal/doNotation.ts Outdated Show resolved Hide resolved
packages/effect/src/Array.ts Outdated Show resolved Hide resolved
KhraksMamtsov and others added 6 commits May 3, 2024 10:18
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
packages/effect/src/Array.ts Outdated Show resolved Hide resolved
packages/effect/src/Array.ts Outdated Show resolved Hide resolved
KhraksMamtsov and others added 2 commits May 3, 2024 11:29
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
@gcanti gcanti merged commit e9a063c into Effect-TS:next-minor May 3, 2024
12 checks passed
github-actions bot pushed a commit that referenced this pull request May 13, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 14, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 14, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 14, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 15, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 15, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 15, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 15, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 15, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 15, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 15, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 16, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 16, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 16, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 16, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 16, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 16, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 16, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 17, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 17, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 17, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 18, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 19, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 19, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
github-actions bot pushed a commit that referenced this pull request May 19, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
tim-smart added a commit that referenced this pull request May 20, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
tim-smart added a commit that referenced this pull request May 20, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
tim-smart added a commit that referenced this pull request May 20, 2024
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
Co-authored-by: Tim <hello@timsmart.co>
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
@KhraksMamtsov KhraksMamtsov deleted the array-do-notation branch June 24, 2024 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants