Skip to content

Instantly share code, notes, and snippets.

@LFReD
Last active December 9, 2019 07:21
Show Gist options
  • Save LFReD/af286d491d18052d55a4833478b006e0 to your computer and use it in GitHub Desktop.
Save LFReD/af286d491d18052d55a4833478b006e0 to your computer and use it in GitHub Desktop.
Atomica for Red 0.2.0
Red [
Title: "Atomica for Red"
Author: "Terry Brownell"
File: %atomica.red
Version: 0.2.3
Rights: "Copyright (C) 2019 Terry W. Brownell"
License: {
Distributed under the BSD-3 License
See https://opensource.org/licenses/BSD-3-Clause
}
]
if not exists? %data/atomica.db [make-dir %data write %data/atomica.db ""]
saveit: does [
dbsave: to block! atomica
save/all %data/atomica.db dbsave
dbsave: copy []
]
loadit: does [
atomica: load %data/atomica.db
atomica: to-hash atomica
]
cleanit: func [s1 p1][trim replace/all s1 " " "" trim replace/all p1 " " ""]
atomexists?: func [s1 p1 v1][
cleanit s1 p1
v1: trim to-string v1
if find/skip atomica reduce [s1 p1 v1] 3[return true]
return false
]
; --- Insert Atom - creates or updates a unique predicate eg: insertatom "bob" "firstname" "Robert"
insertatom: func [s1 p1 v1 /local updated? i i2][
atomica: head atomica
cleanit s1 p1
updated?: false
v1: trim v1
if series? i: find atomica reduce[s1 p1][i2: index? i i2: i2 + 2 atomica/:i2: v1 updated?: true]
if updated? = false [append atomica reduce [s1 p1 v1]]
saveit
]
; --- Insert Atoms - use this for multiple predicates eg: insertaoms "bob" "haspet" "Fido"
insertatoms: func [s1 p1 v1 /local is?][
atomica: head atomica
cleanit s1 p1
v1: trim to-string v1
is?: atomexists? s1 p1 v1
if is? = false [append atomica reduce [s1 p1 v1] saveit]
]
; --- Single Value - Returns value of subject and predicate
singlev: func [s1 p1 /local sub pred][
atomica: head atomica
cleanit s1 p1
if error? try [return third find/skip atomica reduce [s1 p1] 3][return none]
]
; --- Value of Subject and Predicate
vofsandp: func [s1 p1 /local output][
atomica: head atomica
cleanit s1 p1
output: copy []
while [not tail? atomica][if all [atomica/1 = s1 atomica/2 = p1 ][append output atomica/3] atomica: skip atomica 3]
return output
]
; --- Subject of Predicate and Value - Returns unique block of all subjects of specific pred and val
sofpandv: func [p1 v1 /local output][
atomica: head atomica
p1: trim replace/all p1 " " ""
v1: trim v1
output: copy []
while [not tail? atomica][if all [atomica/2 = p1 atomica/3 = v1 ][append output atomica/1] atomica: skip atomica 3]
return output
]
; --- Subject of Predicate - Returns block of unique subjects
sofp: func [p1 /local output][
atomica: head atomica
p1: trim p1: trim replace/all p1 " " ""
output: copy []
while [not tail? atomica][if atomica/2 = p1 [append output atomica/1] atomica: skip atomica 3]
output: unique output
return output
]
; --- Value of Predicate - Returns block of unique values
vofp: func [p1 /local output][
atomica: head atomica
p1: trim p1: trim replace/all p1 " " ""
output: copy []
while [not tail? atomica][if atomica/2 = p1 [append output atomica/3] atomica: skip atomica 3]
output: unique output
return output
]
; --- Delete atom requires the subject, predicate and value of atom to delete.
deleteatom: func [s1 p1 v1 /local flagit][
atomica: head atomica
cleanit s1 p1
v1: trim to-string v1
flagit: false
while [not tail? atomica][if all [atomica/1 = s1 atomica/2 = p1 atomica/3 = v1] [remove/part atomica 3 flagit: true] atomica: skip atomica 3]
if flagit = true [atomica: head atomica saveit]
]
loadit
@ralfwenske
Copy link

Hi Terry, I'm enjoying to play a bit with %atomica.red.
I know your priority is usability with other languages - I see if I can modify it a bit towards Red-land (not that I am an expert though :)
Found an oversight in line 48 (version 0.2.3): db needs to be replaced with atomica

@LFReD
Copy link
Author

LFReD commented Dec 9, 2019

Thought I had all those :) Thanks!
This is the Red version, so any improvements via Red-land is welcome.

For anyone that's interested, there's a Gitter room here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment