Skip to content

Instantly share code, notes, and snippets.

@endo64
Created May 3, 2020 13:41
Show Gist options
  • Save endo64/9b0e27ad3aa1d40a157294b8ae66dd23 to your computer and use it in GitHub Desktop.
Save endo64/9b0e27ad3aa1d40a157294b8ae66dd23 to your computer and use it in GitHub Desktop.
alter.red
b: []
;== []
v1: ["a" "B"]
;== ["a" "B"]
v2: ["A" "b"]
;== ["A" "b"]
alter b v1 b
;== [["a" "B"]]
alter b v2 b
;== []
alter/same b v1 b
;== [["a" "B"]]
alter/same b v2 b
;== [["a" "B"] ["A" "b"]]
alter/case b v2 b
;== [["a" "B"]]
alter b v2 b
;== []
;Added /case and /same refinements to alter function
;https://gitter.im/red/red?at=5eae8a6ea9de3d01b1e5a34c
;https://github.com/red/red/issues/3190
alter: func [
"If a value is not found in a series, append it; otherwise, remove it. Returns true if added"
series [series!]
value
/case "Case-sensitive comparison"
/same {Use "same?" as comparator}
/local find'
][
find': copy either same ['find/same][
either case ['find/case] [to path! 'find]
]
if series? :value [append find' 'only]
not none? unless remove do compose [(find') series :value] [
either series? :value [append/only series :value] [append series :value]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment