September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
41
Task/Object-serialization/Neko/object-serialization.neko
Normal file
41
Task/Object-serialization/Neko/object-serialization.neko
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/* Object serialization, in Neko */
|
||||
|
||||
var file_open = $loader.loadprim("std@file_open", 2)
|
||||
var file_write = $loader.loadprim("std@file_write", 4)
|
||||
var file_read = $loader.loadprim("std@file_read", 4)
|
||||
var file_close = $loader.loadprim("std@file_close", 1)
|
||||
|
||||
var serialize = $loader.loadprim("std@serialize", 1)
|
||||
var unserialize = $loader.loadprim("std@unserialize", 2)
|
||||
|
||||
/* Inheritance by prototype */
|
||||
proto = $new(null)
|
||||
proto.print = function () { $print(this, "\n") }
|
||||
|
||||
obj = $new(null)
|
||||
obj.msg = "Hello"
|
||||
obj.dest = $array("Town", "Country", "World")
|
||||
|
||||
$objsetproto(obj, proto)
|
||||
$print("Original:\n")
|
||||
obj.print()
|
||||
|
||||
/* Serialize the object */
|
||||
var thing = serialize(obj)
|
||||
var len = $ssize(thing)
|
||||
|
||||
/* To disk */
|
||||
var f = file_open("object-serialization.bin", "w")
|
||||
file_write(f, thing, 0, len)
|
||||
file_close(f)
|
||||
|
||||
/* Load the binary data into a new string space */
|
||||
f = file_open("object-serialization.bin", "r")
|
||||
var buff = $smake(len)
|
||||
file_read(f, buff, 0, len)
|
||||
file_close(f)
|
||||
|
||||
/* Unserialize the object into a new variable */
|
||||
var other = unserialize(buff, $loader)
|
||||
$print("deserialized:\n")
|
||||
other.print()
|
||||
32
Task/Object-serialization/Ol/object-serialization.ol
Normal file
32
Task/Object-serialization/Ol/object-serialization.ol
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
$ ol
|
||||
Welcome to Otus Lisp 1.2,
|
||||
type ',help' to help, ',quit' to end session.
|
||||
> (define Object (tuple
|
||||
'(1 2 3 4) ; list
|
||||
#(4 3 2 1) ; bytevector
|
||||
"hello" ; ansi string
|
||||
"こんにちは" ; unicode string
|
||||
(list->ff '(; associative array
|
||||
(1 . 123456)
|
||||
(2 . second)
|
||||
(3 . "-th-")))
|
||||
#false ; value
|
||||
-123 ; short number
|
||||
123456789012345678901234567890123456789 ; long number
|
||||
)
|
||||
;; Defined Object
|
||||
#[(1 2 3 4) #u8(4 3 2 1) hello こんにちは #((1 . 123456) (2 . second) (3 . -th-)) #false -123 123456789012345678901234567890123456789]
|
||||
|
||||
> (fasl-save Object "/tmp/object.bin")
|
||||
#true
|
||||
|
||||
> (define New (fasl-load "/tmp/object.bin" #false))
|
||||
;; Defined New
|
||||
#[(1 2 3 4) #u8(4 3 2 1) hello こんにちは #((1 . 123456) (2 . second) (3 . -th-)) #false -123 123456789012345678901234567890123456789]
|
||||
|
||||
> (equal? Object New)
|
||||
#true
|
||||
|
||||
> ,quit
|
||||
bye-bye :/
|
||||
$
|
||||
36
Task/Object-serialization/Perl-6/object-serialization.pl6
Normal file
36
Task/Object-serialization/Perl-6/object-serialization.pl6
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/env perl6
|
||||
|
||||
# Reference:
|
||||
# https://docs.perl6.org/language/classtut
|
||||
# https://github.com/teodozjan/perl-store
|
||||
|
||||
use v6;
|
||||
use PerlStore::FileStore;
|
||||
|
||||
class Point {
|
||||
has Int $.x;
|
||||
has Int $.y;
|
||||
}
|
||||
|
||||
class Rectangle does FileStore {
|
||||
has Point $.lower;
|
||||
has Point $.upper;
|
||||
|
||||
method area() returns Int {
|
||||
($!upper.x - $!lower.x) * ( $!upper.y - $!lower.y);
|
||||
}
|
||||
}
|
||||
|
||||
my $r1 = Rectangle.new(lower => Point.new(x => 0, y => 0),
|
||||
upper => Point.new(x => 10, y => 10));
|
||||
say "Create Rectangle1 with area ",$r1.area();
|
||||
say "Serialize Rectangle1 to object.dat";
|
||||
$r1.to_file('./objects.dat');
|
||||
say "";
|
||||
say "take a peek on object.dat ..";
|
||||
say slurp "./objects.dat";
|
||||
say "";
|
||||
say "Deserialize to Rectangle2";
|
||||
my $r2 = from_file('objects.dat');
|
||||
say "Rectangle2 is of type ", $r2.WHAT;
|
||||
say "Rectangle2 area is ", $r2.area();
|
||||
37
Task/Object-serialization/Phix/object-serialization.phix
Normal file
37
Task/Object-serialization/Phix/object-serialization.phix
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
include builtins\serialize.e
|
||||
|
||||
function randobj()
|
||||
-- test function (generate some random garbage)
|
||||
object res
|
||||
if rand(10)<=3 then -- make sequence[1..3]
|
||||
res = {}
|
||||
for i=1 to rand(3) do
|
||||
res = append(res,randobj())
|
||||
end for
|
||||
elsif rand(10)<=3 then -- make string
|
||||
res = repeat('A'+rand(10),rand(10))
|
||||
else
|
||||
res = rand(10)/2 -- half int/half float
|
||||
end if
|
||||
return res
|
||||
end function
|
||||
|
||||
object o1 = randobj(),
|
||||
o2 = randobj(),
|
||||
o3 = randobj()
|
||||
|
||||
pp({o1,o2,o3},{pp_Nest,1})
|
||||
integer fh = open("objects.dat", "wb")
|
||||
puts(fh, serialize(o1))
|
||||
puts(fh, serialize(o2))
|
||||
puts(fh, serialize(o3))
|
||||
close(fh)
|
||||
|
||||
?"==="
|
||||
|
||||
fh = open("objects.dat", "rb")
|
||||
?deserialize(fh)
|
||||
?deserialize(fh)
|
||||
?deserialize(fh)
|
||||
close(fh)
|
||||
{} = delete_file("objects.dat")
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
serde = { version = "1.0.89", features = ["derive"] }
|
||||
bincode = "1.1.2"
|
||||
48
Task/Object-serialization/Rust/object-serialization-2.rust
Normal file
48
Task/Object-serialization/Rust/object-serialization-2.rust
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
use std::fmt;
|
||||
|
||||
use bincode::{deserialize, serialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
enum Animal {
|
||||
Dog { name: String, color: String },
|
||||
Bird { name: String, wingspan: u8 },
|
||||
}
|
||||
|
||||
impl fmt::Display for Animal {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Animal::Dog { name, color } => write!(f, "{} is a dog with {} fur", name, color),
|
||||
Animal::Bird { name, wingspan } => {
|
||||
write!(f, "{} is a bird with a wingspan of {}", name, wingspan)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> bincode::Result<()> {
|
||||
let animals = vec![
|
||||
Animal::Dog {
|
||||
name: "Rover".into(),
|
||||
color: "brown".into(),
|
||||
},
|
||||
Animal::Bird {
|
||||
name: "Tweety".into(),
|
||||
wingspan: 3,
|
||||
},
|
||||
];
|
||||
|
||||
for animal in &animals {
|
||||
println!("{}", animal);
|
||||
}
|
||||
|
||||
let serialized = serialize(&animals)?;
|
||||
|
||||
println!("Serialized into {} bytes", serialized.len());
|
||||
|
||||
let deserialized: Vec<Animal> = deserialize(&serialized)?;
|
||||
|
||||
println!("{:#?}", deserialized);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
serde = { version = "1.0.89", features = ["derive"] }
|
||||
bincode = "1.1.2"
|
||||
typetag = "0.1.1"
|
||||
86
Task/Object-serialization/Rust/object-serialization-4.rust
Normal file
86
Task/Object-serialization/Rust/object-serialization-4.rust
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
use std::fmt::{self, Debug, Display};
|
||||
|
||||
use bincode::{deserialize, serialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[typetag::serde(tag = "animal")]
|
||||
trait Animal: Display + Debug {
|
||||
fn name(&self) -> Option<&str>;
|
||||
fn feet(&self) -> u32;
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
struct Dog {
|
||||
name: String,
|
||||
color: String,
|
||||
}
|
||||
|
||||
impl Display for Dog {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{} is a dog with {} fur", self.name, self.color)
|
||||
}
|
||||
}
|
||||
|
||||
#[typetag::serde]
|
||||
impl Animal for Dog {
|
||||
fn name(&self) -> Option<&str> {
|
||||
Some(&self.name)
|
||||
}
|
||||
|
||||
fn feet(&self) -> u32 {
|
||||
4
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
struct Bird {
|
||||
name: String,
|
||||
wingspan: u32,
|
||||
}
|
||||
|
||||
impl Display for Bird {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{} is a bird with a wingspan of {}",
|
||||
self.name, self.wingspan
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[typetag::serde]
|
||||
impl Animal for Bird {
|
||||
fn name(&self) -> Option<&str> {
|
||||
Some(&self.name)
|
||||
}
|
||||
|
||||
fn feet(&self) -> u32 {
|
||||
2
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> bincode::Result<()> {
|
||||
let animals: Vec<Box<dyn Animal>> = vec![
|
||||
Box::new(Dog {
|
||||
name: "Rover".into(),
|
||||
color: "brown".into(),
|
||||
}),
|
||||
Box::new(Bird {
|
||||
name: "Tweety".into(),
|
||||
wingspan: 3,
|
||||
}),
|
||||
];
|
||||
|
||||
for animal in &animals {
|
||||
println!("{}", animal);
|
||||
}
|
||||
|
||||
let serialized = serialize(&animals)?;
|
||||
println!("Serialized into {} bytes", serialized.len());
|
||||
|
||||
let deserialized: Vec<Box<dyn Animal>> = deserialize(&serialized)?;
|
||||
|
||||
println!("{:#?}", deserialized);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue