Explorer

definy.strings-unique

description
The items without the repeated strings, keeping the first of the same strings in the order of items. Uses a hash index of the items in reverse order (core.list-hash-index-find returns the last in its bucket, which is then the first in items), so it takes O(n log n) instead of O(n^2)
type
Fn(items: List<String>) -> List<String>
expression
fn(items$0) =>
  let keyed$1 = @core.list-fold(
      T: String,
      A: Record{position: Number, keyed: List<Record{key: String, position: Number}>},
      reducer: fn(acc$2, item$3) =>
        {
          position: @core.plus(left: $2.position, right: 1),
          keyed: @core.list-append(list: $2.keyed, item: {key: $3, position: $2.position})
        },
      init: {position: 0, keyed: []},
      xs: $0
    );
  let first$4 = @core.list-hash-index(
      list: @core.list-reverse(
        T: Record{key: String, position: Number},
        list: $1.keyed,
        start: 0,
        end: @core.list-length(value: $1.keyed)
      ),
      field: "key"
    );
  let kept$5 = @core.list-fold(
      T: Record{key: String, position: Number},
      A: List<String>,
      reducer: fn(acc$6, item$7) =>
        match @core.list-hash-index-find(index: $4, field: "key", value: $7.key) {
          #some(found$8) =>
            match @core.equal(left: $8.position, right: $7.position) {
              true =>
                @core.list-append(list: $6, item: $7.key),
              false =>
                $6
            },
          #none(_) =>
            $6
        },
      init: [],
      xs: $1.keyed
    );
  $5
visibility
public
definy.strings-unique (project definy, branch release-time-compare, snapshot JJrRgQW_LZN6tN8CbYr1dN7HosqNTuIWWYx7ACbt1n0)
id: ZNkze6DG0miPQHkqyfLbTN8yFk8vEtvcwc3r7OvNXvY
content hash: H1QlOB7zkQFIdVfzjeyHlRO4bGbCUF7s1-UetMoVfGw