Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
79
Task/Simple-database/Bracmat/simple-database.bracmat
Normal file
79
Task/Simple-database/Bracmat/simple-database.bracmat
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
whl
|
||||
' ( arg$:?command
|
||||
& ( get'db
|
||||
| (db=1)&lst$(db,db,NEW)
|
||||
)
|
||||
& !command
|
||||
: ( add
|
||||
& :?name:?tag:?date
|
||||
& whl
|
||||
' ( arg$:?argmnt
|
||||
& arg$:?value
|
||||
& (!argmnt.!value)
|
||||
: ( (title|name.?name)
|
||||
| (category|tag.?tag)
|
||||
| (date.?date)
|
||||
)
|
||||
)
|
||||
& ( !name:~
|
||||
& !tag:~
|
||||
& !date:~
|
||||
& ( !db:?*!tag^(?+(!date.!name)+?)*?
|
||||
& out$"This record already exists"
|
||||
| !tag^(!date.!name)*!db:?db
|
||||
& lst$(db,db,NEW)
|
||||
)
|
||||
| out$"invalid data"
|
||||
)
|
||||
| latest
|
||||
& :?date
|
||||
& nothing found:?latest
|
||||
& ( !db
|
||||
: ?
|
||||
* ?tag
|
||||
^ ( ?
|
||||
+ ( (>!date:?date.?name)
|
||||
& (!name,!tag,!date):?latest
|
||||
& ~
|
||||
)
|
||||
+ ?
|
||||
)
|
||||
* ?
|
||||
| out$!latest
|
||||
)
|
||||
| latest/category
|
||||
& :?date:?latests:?latest
|
||||
& ( !db
|
||||
: ?
|
||||
* ( ?tag
|
||||
& !latests !latest:?latests
|
||||
& :?latest:?date
|
||||
)
|
||||
^ ( ?
|
||||
+ ( (>!date:?date.?name)
|
||||
& (!name,!tag,!date):?latest
|
||||
& ~
|
||||
)
|
||||
+ ?
|
||||
)
|
||||
* ?
|
||||
| !latests !latest:?latests&out$!latests
|
||||
)
|
||||
| sorted
|
||||
& 0:?sorted
|
||||
& ( !db
|
||||
: ?
|
||||
* ?tag
|
||||
^ ( ?
|
||||
+ ( (?date.?name)
|
||||
& (!date.!name,!tag,!date)+!sorted:?sorted
|
||||
& ~
|
||||
)
|
||||
+ ?
|
||||
)
|
||||
* ?
|
||||
| whl
|
||||
' (!sorted:(?.?row)+?sorted&out$!row)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
|
@ -12,7 +12,7 @@ private {
|
|||
if (item.length < 3)
|
||||
return printUsage();
|
||||
auto db = load();
|
||||
const date = text(cast(DateTime)Clock.currTime);
|
||||
const date = (cast(DateTime)Clock.currTime).toISOExtString;
|
||||
const cat = (item.length == 4) ? item[3] : "none";
|
||||
db ~= Item(item[2], date, cat);
|
||||
store(db);
|
||||
|
|
|
|||
125
Task/Simple-database/Java/simple-database.java
Normal file
125
Task/Simple-database/Java/simple-database.java
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
import java.io.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
public class SimpleDatabase {
|
||||
|
||||
final static String filename = "simdb.csv";
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length < 1 || args.length > 3) {
|
||||
printUsage();
|
||||
return;
|
||||
}
|
||||
|
||||
switch (args[0].toLowerCase()) {
|
||||
case "add":
|
||||
addItem(args);
|
||||
break;
|
||||
case "latest":
|
||||
printLatest(args);
|
||||
break;
|
||||
case "all":
|
||||
printAll();
|
||||
break;
|
||||
default:
|
||||
printUsage();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Item implements Comparable<Item>{
|
||||
final String name;
|
||||
final String date;
|
||||
final String category;
|
||||
|
||||
Item(String n, String d, String c) {
|
||||
name = n;
|
||||
date = d;
|
||||
category = c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Item item){
|
||||
return date.compareTo(item.date);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s,%s,%s%n", name, date, category);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addItem(String[] input) {
|
||||
if (input.length < 2) {
|
||||
printUsage();
|
||||
return;
|
||||
}
|
||||
List<Item> db = load();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String date = sdf.format(new Date());
|
||||
String cat = (input.length == 3) ? input[2] : "none";
|
||||
db.add(new Item(input[1], date, cat));
|
||||
store(db);
|
||||
}
|
||||
|
||||
private static void printLatest(String[] a) {
|
||||
List<Item> db = load();
|
||||
if (db.isEmpty()) {
|
||||
System.out.println("No entries in database.");
|
||||
return;
|
||||
}
|
||||
Collections.sort(db);
|
||||
if (a.length == 2) {
|
||||
for (Item item : db)
|
||||
if (item.category.equals(a[1]))
|
||||
System.out.println(item);
|
||||
} else {
|
||||
System.out.println(db.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
private static void printAll() {
|
||||
List<Item> db = load();
|
||||
if (db.isEmpty()) {
|
||||
System.out.println("No entries in database.");
|
||||
return;
|
||||
}
|
||||
Collections.sort(db);
|
||||
for (Item item : db)
|
||||
System.out.println(item);
|
||||
}
|
||||
|
||||
private static List<Item> load() {
|
||||
List<Item> db = new ArrayList<>();
|
||||
try (Scanner sc = new Scanner(new File(filename))) {
|
||||
while (sc.hasNext()) {
|
||||
String[] item = sc.nextLine().split(",");
|
||||
db.add(new Item(item[0], item[1], item[2]));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
return db;
|
||||
}
|
||||
|
||||
private static void store(List<Item> db) {
|
||||
try (FileWriter fw = new FileWriter(filename)) {
|
||||
for (Item item : db)
|
||||
fw.write(item.toString());
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void printUsage() {
|
||||
System.out.println("Usage:");
|
||||
System.out.println(" simdb cmd [categoryName]");
|
||||
System.out.println(" add add item, followed by optional category");
|
||||
System.out.println(" latest print last added item(s), followed by "
|
||||
+ "optional category");
|
||||
System.out.println(" all print all");
|
||||
System.out.println(" For instance: add \"some item name\" "
|
||||
+ "\"some category name\"");
|
||||
}
|
||||
}
|
||||
15
Task/Simple-database/REBOL/simple-database.rebol
Normal file
15
Task/Simple-database/REBOL/simple-database.rebol
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
rebol [author: "Nick Antonaccio"]
|
||||
write/append %rdb "" db: load %rdb
|
||||
switch system/options/args/1 [
|
||||
"new" [write/append %rdb rejoin [now " " mold/only next system/options/args newline]]
|
||||
"latest" [print copy/part tail sort/skip db 4 -4]
|
||||
"latestcat" [
|
||||
foreach cat unique extract at db 3 4 [
|
||||
t: copy []
|
||||
foreach [a b c d] db [if c = cat [append t reduce [a b c d]]]
|
||||
print copy/part tail sort/skip t 4 -4
|
||||
]
|
||||
]
|
||||
"sort" [probe sort/skip db 4]
|
||||
]
|
||||
halt
|
||||
77
Task/Simple-database/REXX/simple-database.rexx
Normal file
77
Task/Simple-database/REXX/simple-database.rexx
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
/* REXX ---------------------------------------------------------------
|
||||
* 05.10.2014
|
||||
*--------------------------------------------------------------------*/
|
||||
x05='05'x
|
||||
mydb='sidb.txt'
|
||||
Say 'Enter your commands, ?, or end'
|
||||
Do Forever
|
||||
Parse Pull l
|
||||
Parse Var l command text
|
||||
Select
|
||||
When command='?' Then
|
||||
Call help
|
||||
When command='add' Then Do
|
||||
Parse Var text item ',' category ',' date
|
||||
If date='' Then
|
||||
date=date('S') /*yyyymmdd*/
|
||||
Say 'adding item' item'/'category 'dated' date
|
||||
Call lineout mydb,date item x05 category
|
||||
End
|
||||
When command='latest' Then Do
|
||||
Call lineout mydb
|
||||
Parse Var text category
|
||||
hidt='00000000'
|
||||
ol=''
|
||||
Do While lines(mydb)>0
|
||||
l=linein(mydb)
|
||||
Parse Var l dt a (x05) b
|
||||
If category=''|,
|
||||
category='-' & b='' |,
|
||||
b=category Then Do
|
||||
If dt>>hidt Then Do
|
||||
ol=l
|
||||
hidt=dt
|
||||
End
|
||||
End
|
||||
End
|
||||
If ol>'' Then
|
||||
Call o ol
|
||||
Else
|
||||
Say 'no matching item found'
|
||||
End
|
||||
When command='all' Then Do
|
||||
Call lineout mydb
|
||||
Parse Var text category
|
||||
Do While lines(mydb)>0
|
||||
l=linein(mydb)
|
||||
Parse Var l a (x05) b
|
||||
If category=''|,
|
||||
category='-' & b=''|,
|
||||
b=category Then
|
||||
Call o l
|
||||
End
|
||||
End
|
||||
When command='end' Then
|
||||
Leave
|
||||
Otherwise Do
|
||||
Say 'invalid command ('command')'
|
||||
Call help
|
||||
End
|
||||
End
|
||||
End
|
||||
Say 'Bye'
|
||||
Exit
|
||||
|
||||
o: Parse Value arg(1) With dt text
|
||||
Say left(dt,8) text
|
||||
Return
|
||||
|
||||
help:
|
||||
Say 'add item[,[category][,date]] to add an item'
|
||||
Say 'latest category to list the latest item of a category'
|
||||
Say 'latest to list the latest item'
|
||||
Say 'all category to list all items of a category'
|
||||
Say 'all to list all items'
|
||||
Say 'end to end this program'
|
||||
Say 'Use category - to list items without category'
|
||||
Return
|
||||
38
Task/Simple-database/Scala/simple-database.scala
Normal file
38
Task/Simple-database/Scala/simple-database.scala
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
object SimpleDatabase extends App {
|
||||
type Entry = Array[String]
|
||||
def asTSV(e: Entry) = e mkString "\t"
|
||||
def fromTSV(s: String) = s split "\t"
|
||||
val header = asTSV(Array("TIMESTAMP", "DESCRIPTION", "CATEGORY", "OTHER"))
|
||||
|
||||
def read(filename: String) = try {
|
||||
scala.io.Source.fromFile(filename).getLines.drop(1).map(fromTSV)
|
||||
} catch {
|
||||
case e: java.io.FileNotFoundException => Nil
|
||||
}
|
||||
|
||||
def write(filename: String, all: Seq[Entry]) = {
|
||||
import java.nio.file.{Files,Paths}
|
||||
import scala.collection.JavaConversions.asJavaIterable
|
||||
Files.write(Paths.get(filename), asJavaIterable(header +: all.map(asTSV)))
|
||||
all.size
|
||||
}
|
||||
|
||||
def add(filename: String, description: String, category: String = "none", optional: Seq[String] = Nil) {
|
||||
val format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
|
||||
val e = Array(format.format(new java.util.Date), description, category) ++ optional
|
||||
println(write(filename, read(filename).toBuffer :+ e) + " entries")
|
||||
}
|
||||
|
||||
def print(filename: String, filter: Seq[Entry] => TraversableOnce[Entry]) =
|
||||
filter(read(filename).toList.sortBy(_.headOption)) map(_ mkString ",") foreach println
|
||||
|
||||
args match {
|
||||
case Array(f, "latest") => print(f, _ takeRight 1)
|
||||
case Array(f, "latest", cat) => print(f, _ filter(_.lift(2) == Some(cat)) takeRight 1)
|
||||
case Array(f, "all") => print(f, _.toSeq)
|
||||
case Array(f, "all", "latest") => print(f, _ groupBy (_ lift 2 getOrElse "") map{case (_, cat) => cat.last})
|
||||
case Array(f, "add", desc) => add(f, desc, category = "")
|
||||
case Array(f, "add", desc, cat, opt @ _*) => add(f, desc, cat, opt)
|
||||
case _ => println("Usage: SimpleDatabase filename.tsv [all [latest]| latest [CATEGORY] | add [DESCRIPTION [CATEGORY [OPTIONAL]...]]]")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue