Jump to content

Simple database: Difference between revisions

(Insert J solution)
Line 243:
 
=={{header|J}}==
 
J comes with a sql database, jdb. Jdb's columns are memory mapped files with header information. These won't meet the human readable data file requirement. Hence, this program:
 
<lang j>HELP=: 0 :0
Commands:
 
DBNAME add DATA
DBNAME display the latest entry
DBNAME display the latest entry where CATEGORY contains WORD
DBNAME display all entries
DBNAME display all entries order by CATEGORY
 
1) The first add with new DBNAME assign category names.
2) lower case arguments verbatim.
3) UPPER CASE: substitute your values.
 
Examples, having saved this program as a file named s :
$ jconsole s simple.db display all entries
$ jconsole s simple.db add "first field" "2nd field"
)
 
Q=: '''' NB. quote character
time=: 6!:0@:('YYYY-MM-DD:hh:mm:ss.sss'"_)
 
embed=: >@:{.@:[ , ] , >@:{:@:[
assert '(x+y)' -: '()' embed 'x+y'
 
Duplicate=: 1 :'#~ m&= + 1 #~ #'
assert 0 1 2 3 3 4 -: 3 Duplicate i.5
 
prepare=: LF ,~ [: }:@:; (Q;Q,';')&embed@:(Q Duplicate)&.>@:(;~ time)
assert (-: }.@:".@:}:@:prepare) 'boxed';'';'li''st';'of';'''strings'''
 
categorize=: dyad define
i=. x i. y
if. (1 (~: #) i) +. i (= #) x do.
smoutput 'choose 1 of'
smoutput x
exit 1
end.
{. i NB. "index of" frame has rank 1.
)
assert 0 -: 'abc' categorize 'a'
 
loadsdb=: (<'time') (<0 0)} ".;._2@:(1!:1)
 
Dispatch=: conjunction define
help
:
commands=. y
command=. {. commands
x (u`help@.(command(i.~ ;:)n)) }.commands
)
 
NB. the long fork in show groups (": #~ (1 1 embed (1j1 }:@:# (1 #~ #))))
show=: smoutput@:(": #~ 1 1 embed 1j1 }:@:# 1 #~ #)
 
in=: +./@:E.
assert 'the' in'quick brown fox jumps over the lazy dog'
assert 'the'-.@:in'QUICK BROWN FOX JUMPS OVER THE LAZY DOG'
 
where=: dyad define
'category contains word'=. 3 {. y
if. 'contains' -.@:-: contains do.
help''
exit 1
end.
i=. x ({.@:[ categorize <@:]) category
j=. {: I. ; word&in&.> i {"1 x
if. 0 (= #) j do.
smoutput 'no matches'
else.
x (show@:{~ 0&,) j
end.
)
 
entry=: 4 : 0
if. a: = y do.
show@:({. ,: {:) x
else.
x ''`where Dispatch'where' y
end.
)
 
latest=: ''`entry Dispatch'entry'
the=: ''`latest Dispatch'latest'
 
by=: 4 : 0
i=. x (categorize~ {.)~ y
show ({. , (/: i&{"1)@:}.) x
)
 
order=: ''`by Dispatch'by'
 
entries=: 4 : 0
if. a: = y do.
show x
else.
x ''`order Dispatch'order' y
end.
)
 
all=: ''`entries Dispatch'entries'
 
help=: smoutput@:(HELP"_)
add=: 1!:3~ prepare NB. minimal---no error tests
display=: (the`all Dispatch'the all'~ loadsdb)~ NB. load the simple db for some sort of display
 
({. add`display Dispatch'add display' }.)@:(2&}.)ARGV
 
exit 0
</lang>
Assume the j code is stored in file s . These bash commands, stored in file input , create a database using add .
<lang sh>D='jconsole s dataflow'
 
$D add name expression algebraic rank valence example explanation
$D add insert 'f/ y' 'insert f within y' infinite dyad 'sum=: +/' 'continued_fraction=:+`%/'
$D add fork '(f g h)y' 'g(f(y),h(y))' infinite monad 'average=: +/ % #' 'sum divided by tally'
$D add hook '(f g)y' 'f(y,g(y))' infinite monad '/: 2&{"1' 'sort table by third column'
$D add hook 'x(f g)y' 'f(x,g(y))' infinite dyad 'display verb in s' 'a reflexive dyadic hook'
$D add fork 'x(f g h)y' 'g(f(x,y),h(x,y))' infinite monad '+ * -' 'product of sum and difference'
$D add reflexive 'f~ y' 'f(y,y)' infinite monad '^~y' 'y raised to the power of y'
$D add passive 'x f~ y' 'f(y,x)' 'ranks of f' dyad '(%~ i.@:>:) 8x' '8 intervals from 0 to 1'
$D add atop 'f@g y' 'f(g(y))' 'rank of g' monad '*:@(+/)' 'square the sum'
$D add atop 'x f@g y' 'f(g(x,y))' 'rank of g' dyad '>@{.' '(lisp) open the car'
$D add 'many more!'
</lang>
Now we look up data from the bash command line.
<lang sh>
$ . input # source the input
$ echo $D
jconsole s dataflow
$ $D display the latest entry
┌───────────────────────┬──────────┬──────────┬─────────┬────┬───────┬───────┬───────────┐
│time │name │expression│algebraic│rank│valence│example│explanation│
│2012-02-07:20:36:54.749│many more!│ │ │ │ │ │ │
└───────────────────────┴──────────┴──────────┴─────────┴────┴───────┴───────┴───────────┘
$ $D display the latest entry where 'part of speech' contains verb
choose 1 of
┌────┬────┬──────────┬─────────┬────┬───────┬───────┬───────────┐
│time│name│expression│algebraic│rank│valence│example│explanation│
└────┴────┴──────────┴─────────┴────┴───────┴───────┴───────────┘
$ $D display the latest entry where example contains average
┌───────────────────────┬────┬──────────┬────────────┬────────┬───────┬────────────────┬────────────────────┐
│time │name│expression│algebraic │rank │valence│example │explanation │
│2012-02-07:20:36:54.564│fork│(f g h)y │g(f(y),h(y))│infinite│monad │average=: +/ % #│sum divided by tally│
└───────────────────────┴────┴──────────┴────────────┴────────┴───────┴────────────────┴────────────────────┘
$ $D display all entries ordre by valence # oops! transposition typo.
Commands:
 
DBNAME add DATA
DBNAME display the latest entry
DBNAME display the latest entry where CATEGORY contains WORD
DBNAME display all entries
DBNAME display all entries order by CATEGORY
 
1) The first add with new DBNAME assign category names.
2) lower case arguments verbatim.
3) UPPER CASE: substitute your values.
 
Examples, having saved this program as a file named s :
$ jconsole s simple.db display all entries
$ jconsole s simple.db add "first field" "2nd field"
 
$ $D display all entries order by valence
┌───────────────────────┬──────────┬──────────┬─────────────────┬──────────┬───────┬─────────────────┬─────────────────────────────┐
│time │name │expression│algebraic │rank │valence│example │explanation │
│2012-02-07:20:36:54.749│many more!│ │ │ │ │ │ │
│2012-02-07:20:36:54.541│insert │f/ y │insert f within y│infinite │dyad │sum=: +/ │continued_fraction=:+`%/ │
│2012-02-07:20:36:54.612│hook │x(f g)y │f(x,g(y)) │infinite │dyad │display verb in s│a reflexive dyadic hook │
│2012-02-07:20:36:54.682│passive │x f~ y │f(y,x) │ranks of f│dyad │(%~ i.@:>:) 8x │8 intervals from 0 to 1 │
│2012-02-07:20:36:54.727│atop │x f@g y │f(g(x,y)) │rank of g │dyad │>@{. │(lisp) open the car │
│2012-02-07:20:36:54.564│fork │(f g h)y │g(f(y),h(y)) │infinite │monad │average=: +/ % # │sum divided by tally │
│2012-02-07:20:36:54.589│hook │(f g)y │f(y,g(y)) │infinite │monad │/: 2&{"1 │sort table by third column │
│2012-02-07:20:36:54.635│fork │x(f g h)y │g(f(x,y),h(x,y)) │infinite │monad │+ * - │product of sum and difference│
│2012-02-07:20:36:54.660│reflexive │f~ y │f(y,y) │infinite │monad │^~y │y raised to the power of y │
│2012-02-07:20:36:54.705│atop │f@g y │f(g(y)) │rank of g │monad │*:@(+/) │square the sum │
└───────────────────────┴──────────┴──────────┴─────────────────┴──────────┴───────┴─────────────────┴─────────────────────────────┘
$ $D display all entries
┌───────────────────────┬──────────┬──────────┬─────────────────┬──────────┬───────┬─────────────────┬─────────────────────────────┐
│time │name │expression│algebraic │rank │valence│example │explanation │
│2012-02-07:20:36:54.541│insert │f/ y │insert f within y│infinite │dyad │sum=: +/ │continued_fraction=:+`%/ │
│2012-02-07:20:36:54.564│fork │(f g h)y │g(f(y),h(y)) │infinite │monad │average=: +/ % # │sum divided by tally │
│2012-02-07:20:36:54.589│hook │(f g)y │f(y,g(y)) │infinite │monad │/: 2&{"1 │sort table by third column │
│2012-02-07:20:36:54.612│hook │x(f g)y │f(x,g(y)) │infinite │dyad │display verb in s│a reflexive dyadic hook │
│2012-02-07:20:36:54.635│fork │x(f g h)y │g(f(x,y),h(x,y)) │infinite │monad │+ * - │product of sum and difference│
│2012-02-07:20:36:54.660│reflexive │f~ y │f(y,y) │infinite │monad │^~y │y raised to the power of y │
│2012-02-07:20:36:54.682│passive │x f~ y │f(y,x) │ranks of f│dyad │(%~ i.@:>:) 8x │8 intervals from 0 to 1 │
│2012-02-07:20:36:54.705│atop │f@g y │f(g(y)) │rank of g │monad │*:@(+/) │square the sum │
│2012-02-07:20:36:54.727│atop │x f@g y │f(g(x,y)) │rank of g │dyad │>@{. │(lisp) open the car │
│2012-02-07:20:36:54.749│many more!│ │ │ │ │ │ │
└───────────────────────┴──────────┴──────────┴─────────────────┴──────────┴───────┴─────────────────┴─────────────────────────────┘
$ </lang>
 
=={{header|PicoLisp}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.