Skip to content

Instantly share code, notes, and snippets.

@danielbayley
Last active December 1, 2024 01:58
Show Gist options
  • Select an option

  • Save danielbayley/f2d5adbc5ef182c6aad67f218890c981 to your computer and use it in GitHub Desktop.

Select an option

Save danielbayley/f2d5adbc5ef182c6aad67f218890c981 to your computer and use it in GitHub Desktop.

Revisions

  1. danielbayley revised this gist Dec 1, 2024. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion load.yaml
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,11 @@ add: &add [4, 5]
    total:: reduce(+ *add) # 9

    # Merge
    concat:: concat(*array *add) # 1–5
    extend:
    <<: *object
    d: 4
    merge:: merge(*object { :d 4 }) # a: 1, b: 2, c: 3, d: 4
    concat:: concat(*array *add) # 1–5

    # Flatten
    nested: &nested [0, *array]
  2. danielbayley revised this gist Nov 29, 2024. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions load.yaml
    Original file line number Diff line number Diff line change
    @@ -42,11 +42,11 @@ flatten:: flatten(*nested) # 0–3

    # Paths
    pwd:: CWD.replace(ENV.HOME, "~") # ~/path/to/load.ys
    glob:: fs-glob("*") # []
    dirname:: fs-dirname(FILE) # ~/path/to
    basename:: fs-filename(FILE) # load.ys
    filename:: fs-filename(FILE):fs/strip-ext # load
    extension:: split(FILE "\."):last # ys
    glob:: fs-glob("*") # []
    dirname:: fs-dirname(FILE) # ~/path/to
    basename:: fs-filename(FILE) # load.ys
    filename:: fs-filename(FILE):fs/strip-ext # load
    extension:: fs/split-ext(FILE):last # ys

    # Define
    repository:
  3. danielbayley revised this gist Nov 29, 2024. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions load.yaml
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    # https://npm.im/@yaml/yamlscript
    # https://yamlscript.org/doc/cheat
    # https://yamlscript.org/doc/core
    # https://yamlscript.org/doc/ys-std

    # ys --yaml */load.ys #--json
    # tail -n+2 < */load.ys | pnpx yaml --single --json --indent ${TABSIZE-2}
    @@ -43,8 +44,8 @@ flatten:: flatten(*nested) # 0–3
    pwd:: CWD.replace(ENV.HOME, "~") # ~/path/to/load.ys
    glob:: fs-glob("*") # []
    dirname:: fs-dirname(FILE) # ~/path/to
    basename:: &basename fs-filename(FILE) # load.ys
    filename:: split(*basename "\."):butlast:last # load
    basename:: fs-filename(FILE) # load.ys
    filename:: fs-filename(FILE):fs/strip-ext # load
    extension:: split(FILE "\."):last # ys

    # Define
  4. danielbayley revised this gist Nov 28, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions load.yaml
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,7 @@ flatten:: flatten(*nested) # 0–3

    # Paths
    pwd:: CWD.replace(ENV.HOME, "~") # ~/path/to/load.ys
    glob:: fs-glob("*") # []
    dirname:: fs-dirname(FILE) # ~/path/to
    basename:: &basename fs-filename(FILE) # load.ys
    filename:: split(*basename "\."):butlast:last # load
  5. danielbayley revised this gist Nov 28, 2024. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions load.yaml
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,6 @@ map-object:: map((fn [[k, v]] [k inc(v)]) *object):omap # a: 1, b: 2 c: 3
    # Filter
    filter:: filter(even? *array) # 2
    filter-object:: dissoc(*object "b") # a: 1, c: 3
    #filter((fn [[k v]] (not (in? k ["b"]))) *object):omaps
    grep:: grep((has? ["b"]) *keys) # b

    # Reduce
    @@ -44,10 +43,7 @@ flatten:: flatten(*nested) # 0–3
    pwd:: CWD.replace(ENV.HOME, "~") # ~/path/to/load.ys
    dirname:: fs-dirname(FILE) # ~/path/to
    basename:: &basename fs-filename(FILE) # load.ys
    #defn basename(path): TODO
    #filename:: basename(FILE).split("."):first
    filename:: split(*basename "\."):butlast:last # load
    #filename:: split(FILE "[/.]"):butlast:last
    extension:: split(FILE "\."):last # ys

    # Define
  6. danielbayley revised this gist Nov 28, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions load.yaml
    Original file line number Diff line number Diff line change
    @@ -42,8 +42,8 @@ flatten:: flatten(*nested) # 0–3

    # Paths
    pwd:: CWD.replace(ENV.HOME, "~") # ~/path/to/load.ys
    dirname:: split(FILE "/"):butlast:last # ~/path/to
    basename:: &basename split(FILE "/"):last # load.ys
    dirname:: fs-dirname(FILE) # ~/path/to
    basename:: &basename fs-filename(FILE) # load.ys
    #defn basename(path): TODO
    #filename:: basename(FILE).split("."):first
    filename:: split(*basename "\."):butlast:last # load
  7. danielbayley revised this gist Nov 28, 2024. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions load.yaml
    Original file line number Diff line number Diff line change
    @@ -64,8 +64,10 @@ url =: &url "https://raw.githubusercontent.com/$(*repository)/refs/heads/$(*bra
    fetch =: json/load(curl("$(*url)/package.json"))
    name:: fetch.name # semver-spec

    # Fetch Line
    pattern:: slurp("$(*url)/semver.md").lines().nth(356) #?plain=1#L357
    # Fetch Line(s)
    markdown =: slurp("$(*url)/semver.md") #?plain=1#L7-L15
    summary:: markdown.lines().subvec(6 16).join("\n")
    pattern:: markdown.lines().nth(356)

    # Recursive
    this =: yaml/load(slurp(FILE).lines().drop(3).join("\n"))
  8. danielbayley revised this gist Nov 26, 2024. No changes.
  9. danielbayley revised this gist Nov 26, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion .gitattributes
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    *.ys linguist-language=yaml
  10. danielbayley renamed this gist Nov 26, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. danielbayley renamed this gist Nov 26, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. danielbayley revised this gist Nov 26, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions .gitattributes
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    *.ys linguist-language=yaml
  13. danielbayley created this gist Nov 26, 2024.
    72 changes: 72 additions & 0 deletions load.ys
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #! /usr/bin/env ys-0
    !yamlscript/v0/data

    # https://eemeli.org/yaml#yaml
    # https://npm.im/@yaml/yamlscript
    # https://yamlscript.org/doc/cheat
    # https://yamlscript.org/doc/core

    # ys --yaml */load.ys #--json
    # tail -n+2 < */load.ys | pnpx yaml --single --json --indent ${TABSIZE-2}

    # Convert
    object: &object
    a: 1
    b: 2
    c: 3
    keys:: &keys keys(*object) # a–c
    values:: &array vals(*object) # 1–3
    object->entries:: &entries into([] *object) # [[a, 1], [b, 2], [c, 3]]
    entries->object:: omap(*entries) # a: 1, b: 2 c: 3

    # Map
    map:: map(inc *array) # 2–4
    map-object:: map((fn [[k, v]] [k inc(v)]) *object):omap # a: 1, b: 2 c: 3

    # Filter
    filter:: filter(even? *array) # 2
    filter-object:: dissoc(*object "b") # a: 1, c: 3
    #filter((fn [[k v]] (not (in? k ["b"]))) *object):omaps
    grep:: grep((has? ["b"]) *keys) # b

    # Reduce
    add: &add [4, 5]
    total:: reduce(+ *add) # 9

    # Merge
    concat:: concat(*array *add) # 1–5

    # Flatten
    nested: &nested [0, *array]
    flatten:: flatten(*nested) # 0–3

    # Paths
    pwd:: CWD.replace(ENV.HOME, "~") # ~/path/to/load.ys
    dirname:: split(FILE "/"):butlast:last # ~/path/to
    basename:: &basename split(FILE "/"):last # load.ys
    #defn basename(path): TODO
    #filename:: basename(FILE).split("."):first
    filename:: split(*basename "\."):butlast:last # load
    #filename:: split(FILE "[/.]"):butlast:last
    extension:: split(FILE "\."):last # ys

    # Define
    repository:
    - &owner semver
    - &repo semver
    - &branch master #main

    # Interpolation
    repo =: &repository "$(*owner)/$(*repo)"
    url =: &url "https://raw.githubusercontent.com/$(*repository)/refs/heads/$(*branch)"

    # Fetch Data
    fetch =: json/load(curl("$(*url)/package.json"))
    name:: fetch.name # semver-spec

    # Fetch Line
    pattern:: slurp("$(*url)/semver.md").lines().nth(356) #?plain=1#L357

    # Recursive
    this =: yaml/load(slurp(FILE).lines().drop(3).join("\n"))
    recursive:: this.nested:first # 0