Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
99
Task/Simple-database/Phix/simple-database.phix
Normal file
99
Task/Simple-database/Phix/simple-database.phix
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
--
|
||||
-- demo\rosetta\Simple_db.exw
|
||||
-- ==========================
|
||||
--
|
||||
include timedate.e
|
||||
|
||||
constant filename = getenv(iff(platform()=WINDOWS?"APPDATA":"HOME"))&"/simple_db.csv"
|
||||
|
||||
procedure add(sequence cmd)
|
||||
if length(cmd)=0
|
||||
or length(cmd)>2 then
|
||||
printf(1,"usage: add name [cat]\n")
|
||||
else
|
||||
string name = cmd[1]
|
||||
string cat = iff(length(cmd)=2?cmd[2]:"none")
|
||||
string datestr = format_timedate(date(),"YYYY/MM/DD h:mmpm")
|
||||
integer fn = open(filename,"a")
|
||||
printf(fn,"%s,%s,%s\n",{name,cat,datestr})
|
||||
close(fn)
|
||||
end if
|
||||
end procedure
|
||||
|
||||
procedure last(sequence cmd)
|
||||
integer fn = open(filename,"r")
|
||||
if fn=-1 then
|
||||
puts(1,"file not found\n")
|
||||
return
|
||||
end if
|
||||
integer lc = length(cmd)
|
||||
string last = iff(lc?"<no entries for that category>\n":"<empty>\n")
|
||||
while 1 do
|
||||
object line = gets(fn)
|
||||
if atom(line) then exit end if
|
||||
if lc=0 or split(line,',')[2]=cmd[1] then
|
||||
last = line
|
||||
end if
|
||||
end while
|
||||
puts(1,last)
|
||||
close(fn)
|
||||
end procedure
|
||||
|
||||
sequence dates
|
||||
|
||||
function by_date(integer d1, integer d2)
|
||||
return compare(dates[d1],dates[d2])
|
||||
end function
|
||||
constant r_by_date = routine_id("by_date")
|
||||
|
||||
procedure sort_by_date()
|
||||
-- (simple_db.csv should be edited manually to prove the date sort works)
|
||||
integer fn = open(filename,"r")
|
||||
if fn=-1 then
|
||||
puts(1,"file not found\n")
|
||||
return
|
||||
end if
|
||||
sequence lines = {}
|
||||
dates = {}
|
||||
while 1 do
|
||||
object line = gets(fn)
|
||||
if atom(line) then exit end if
|
||||
lines = append(lines,line)
|
||||
dates = append(dates,split(line,',')[3])
|
||||
end while
|
||||
close(fn)
|
||||
sequence tags = custom_sort(r_by_date,tagset(length(lines)))
|
||||
for i=1 to length(tags) do
|
||||
puts(1,lines[tags[i]])
|
||||
end for
|
||||
end procedure
|
||||
|
||||
procedure process(sequence cmd)
|
||||
switch cmd[1] do
|
||||
case "add": add(cmd[2..$])
|
||||
case "last": last(cmd[2..$])
|
||||
case "sort": sort_by_date()
|
||||
default: printf(1,"unknown command: %s\n",{cmd[1]})
|
||||
end switch
|
||||
end procedure
|
||||
|
||||
constant helptext = """
|
||||
p demo\rosetta\Simple_db -- interactive mode, commands as below
|
||||
p demo\rosetta\Simple_db add name [cat] -- add entry
|
||||
p demo\rosetta\Simple_db last [cat] -- show last entry [in specified category]
|
||||
p demo\rosetta\Simple_db sort -- show full list sorted by date
|
||||
"""
|
||||
sequence cl = command_line()
|
||||
if length(cl)<3 then
|
||||
-- interactive mode
|
||||
puts(1,helptext)
|
||||
while 1 do
|
||||
puts(1,">")
|
||||
object line = trim(gets(0))
|
||||
if atom(line) or length(line)=0 then exit end if
|
||||
puts(1,"\n")
|
||||
process(split(line))
|
||||
end while
|
||||
else
|
||||
process(cl[3..$])
|
||||
end if
|
||||
85
Task/Simple-database/ToffeeScript/simple-database.toffee
Normal file
85
Task/Simple-database/ToffeeScript/simple-database.toffee
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
#!/usr/local/bin/toffee
|
||||
|
||||
prog = require 'commander'
|
||||
fs = require 'fs-extra'
|
||||
|
||||
if not fs.exists! 'data.json'
|
||||
fs.outputJson! 'data.json', {}
|
||||
|
||||
prog
|
||||
.command('add <name> <category> [date]')
|
||||
.description('Add a new entry')
|
||||
.option('-n <text>', 'notes')
|
||||
.option('-t <tags>', 'tags')
|
||||
.action addentry
|
||||
|
||||
prog
|
||||
.command('latest')
|
||||
.description('Print the latest entry')
|
||||
.action latest
|
||||
|
||||
prog
|
||||
.command('catlatest')
|
||||
.description('Print the latest entry for each category')
|
||||
.action catlatestout
|
||||
|
||||
prog
|
||||
.command('list')
|
||||
.description('Print all entries sorted by date')
|
||||
.action bydate
|
||||
|
||||
|
||||
addentry = (name, category, dt, options) ->
|
||||
if dt? then dat = new Date(dt) else dat = new Date()
|
||||
update =
|
||||
name: name
|
||||
category: category
|
||||
tags: options?.T
|
||||
notes: options?.N
|
||||
date: dat.getTime()
|
||||
e, data = fs.readJson! 'data.json'
|
||||
if not data[category]?
|
||||
data[category] = []
|
||||
data[category].push update
|
||||
fs.outputJson 'data.json', data
|
||||
|
||||
byDateNew = (a, b) ->
|
||||
if a.date<b.date then return 1
|
||||
if b.date>a.date then return -1
|
||||
return 0
|
||||
|
||||
catlatest = (cb) ->
|
||||
e, data = fs.readJson! 'data.json'
|
||||
ret = []
|
||||
for cat, list of data
|
||||
list.sort byDateNew
|
||||
ret.push list[0]
|
||||
cb ret
|
||||
|
||||
printFormatted = (entry) ->
|
||||
tmp = entry.date
|
||||
entry.date = new Date(entry.date).toDateString()
|
||||
console.log entry
|
||||
entry.date = tmp
|
||||
|
||||
latest = ->
|
||||
newpercat = catlatest!
|
||||
newpercat.sort byDateNew
|
||||
printFormatted newpercat[0]
|
||||
|
||||
catlatestout = ->
|
||||
items = catlatest!
|
||||
for item in items
|
||||
printFormatted item
|
||||
|
||||
bydate = ->
|
||||
e, data = fs.readJson! 'data.json'
|
||||
entries = []
|
||||
for cat, list of data
|
||||
for item in list
|
||||
entries.push item
|
||||
entries.sort byDateNew
|
||||
for entry in entries
|
||||
printFormatted entry
|
||||
|
||||
prog.parse process.argv
|
||||
Loading…
Add table
Add a link
Reference in a new issue