geektimes.ru/post/100171 :
この哀れなことに、私は新しいプログラミング言語の例を紹介します。
以下に小さなコード例を示します。
[ コードは次の記事のアイデアに基づいています 。 ] (予約文字の意味を推測し、あなたが知っているプログラミング言語でこのコードを精神的に書き換えてみてください。)
そして、より大きな例があります(これは
、以前の記事の Pythonコードの翻訳です)。
[予約済み]レターを使用したコードF calculate_sacred_number()
A results = []
L(hash_algorithm) hashlib:algorithms_available // -
\\ ( MD5, SHA...)
I.unlikely "shake" C hash_algorithm // SHAKE, ...
L.continue
L(uppercase) 0B..1B // ,
L(space) 0B..1B //
L(n) 10..99 //
A nw = :numbers[n] // , .
I uppercase
nw .= upper()
I space
nw .= replace(‘-’, ‘ ’)
A ns = String(n)
// ,
A digest1 = hashlib:(hash_algorithm, nw.encode()).hexdigest()
// , .
A digest2 = hashlib:(hash_algorithm, ns.encode()).hexdigest()
L 2 // ,
//
// .
I digest1[0] == ns[0] & digest1.last == ns[1]
& digest2[0] == ns[0] & digest2.last == ns[1]
results [+]= ns
//
digest1 = digest1[0 .< (len) I/ 2]
digest2 = digest2[0 .< (len) I/ 2]
assert(results.len == 1) // ""
R results[0] //
// Based on [https://stackoverflow.com/a/8982279/2692494 ‘How do I tell Python to convert integers into words’]
A numbers = "zero one two three four five six seven eight nine".split()
numbers [+]= "ten eleven twelve thirteen fourteen fifteen sixteen".split()
numbers [+]= "seventeen eighteen nineteen".split()
L(tens) "twenty thirty forty fifty sixty seventy eighty ninety".split()
L(ones) numbers[0..9]
numbers [+]= I ones == "zero" {tens} E tens‘-’ones
print(calculate_sacred_number())
, []fn calculate_sacred_number()
var results = []
loop(hash_algorithm) hashlib:algorithms_available // -
\\ ( MD5, SHA...)
if.unlikely "shake" in hash_algorithm // SHAKE, ...
loop.continue
loop(uppercase) 0B..1B // ,
loop(space) 0B..1B //
loop(n) 10..99 //
var nw = :numbers[n] // , .
if uppercase
nw .= upper()
if space
nw .= replace(‘-’, ‘ ’)
var ns = String(n)
// ,
var digest1 = hashlib:(hash_algorithm, nw.encode()).hexdigest()
// , .
var digest2 = hashlib:(hash_algorithm, ns.encode()).hexdigest()
loop 2 // ,
//
// .
if digest1[0] == ns[0] & digest1.last == ns[1]
& digest2[0] == ns[0] & digest2.last == ns[1]
results [+]= ns
//
digest1 = digest1[0 .< (len) I/ 2]
digest2 = digest2[0 .< (len) I/ 2]
assert(results.len == 1) // ""
return results[0] //
// Based on [https://stackoverflow.com/a/8982279/2692494 ‘How do I tell Python to convert integers into words’]
var numbers = "zero one two three four five six seven eight nine".split()
numbers [+]= "ten eleven twelve thirteen fourteen fifteen sixteen".split()
numbers [+]= "seventeen eighteen nineteen".split()
loop(tens) "twenty thirty forty fifty sixty seventy eighty ninety".split()
loop(ones) numbers[0..9]
numbers [+]= if ones == "zero" {tens} else tens‘-’ones
print(calculate_sacred_number())
, —
_:_(...)
. ,
fs:path:dirname()
,
re:compile()
\
:()
,
math:log()
\
:()
,
time:sleep()
\
:()
,
json:load()
,
html:escape()
(
fs
,
fs:path
\
:
,
re
,
math
,
time
,
json
,
html
).
—
.
[ : '""'
, ''"" "'""''
, ``.] ,
Python , , , Python, — , (
\closure ), ( Cython :
"http://...?rev=" + revision
).
11/12/13 /:( — , .)
/ , (
[ [?]] ), , 11/12/13 / /.
- ( ++ RAII) —
F.on_scope_exit
( defer Go). - / —
F.destructor
.
.
close():
tmpfile, fname = tempfile.mkstemp(text=True)
tmpfile = open(tmpfile)
r = subprocess.call(cmd, stdout = tmpfile, stderr = tmpfile)
tmpfile.seek(0)
print(tmpfile.read(), end = '')
tmpfile.close()
os.remove(fname)
move() :
Person p
p.name = ...
p.age = ...
persons.append(p) // ++ persons.push_back(std::move(p))
sorted()\():
for root, dirs, files in os.walk(path):
for file in sorted(files):
...
(type inference)
, copy()\() share()\().
(
Nemerle ). autounishared_ptr — unique_ptr shared_ptr C++11. share(p), p shared_ptr-, unique_ptr-.
copy()\() , , , (
=
).
, (a1 a2 ):
a2 = a1;
C++ . Python
a2
a1
. :
a2 = copy(a1)
,
a2 = share(a1)
.
( — , ,
C++ Rust ( ).)
, , ?
A fstr = File(fname).read() // = ().()
File(fname).write(contents) // ().()
I File(fname) // I fs:is_file(fname)
File(fname).size // fs:stat(fname).size
/
Release/Optimized ? Debug ? Debug Release/Optimized? , , / , . , - , Release/Optimized build. , gdb,
optimized out . , - ( - ) ! — . , , , .
, , , , , .
( )
:
(
:numbers
).
—
.
(, ,
.x
[/] x
), ( \static C++) —
.:
.
@ ( C a —
Capture\
) — , nonlocal Python.
^
— ‘ ’\scope, (,
i
, - ,
i
,
i
,
^i
). ^ :
F outer_func(...)
F local_func(...)
^R // (or ^(outer_func)R) return from outer_func
( , ). - , .
P.S. .T Person
String name
Int age
F (name, age)
.name = name // or (.).name = name, because (.) is this/self
.age = age
A persons = []
persons [+]= Person("Name", 17)
// Translation of Python's `def parenthesize(s: Union[str, bytes]) -> Union[str, bytes]: ...` from [http://neopythonic.blogspot.com]:
F parenthesize(T C (String, Bytes) s)
...
// Doubly linked list
T DLListItem[T Type]
Type& prev // ‘unsafe pointer’/‘unowned reference’ [true weak pointer/reference `Type??` is much more expensive]
Type? next
F is_in_list // if method is defined without parentheses, than it must be called also without, i.e.: I it.is_in_list {...}
R next != N
T DLList[T Type(DLListItem)] // Type(DLListItem) means that Type must be derived from DLListItem
Type? first
Type& last
F.destructor // destructor is needed/necessary because some list item may be shared, and in that case it will not be removed from the list and also all following items will not be removed
.clear()
F clear()
Type? p = move(first) // this also sets `first` to N
L p
p.prev = N
Type? n = move(p.next) // this also sets `p.next` to N
p = n // move(n) is not needed here as the compiler put `move` automatically at all places of last use of variable
last = N
F append, [+]=(Type &item) // define both method `append()` and operator [+]=
item.prev = .last
I .last {.last.next = item} E .first = item
.last = item
F calc_len()
Int len = 0
L (.)
len++
R len
F L
F () -> Type& // returns iterator to the first element of this container
R .first
F next(it) -> Type&
R it.next
F prev(it)
R it.prev
(, )
. = // (.). = , (.) this/self
. =
_ = []
_ [+]= ("", 17)
// Python- `def parenthesize(s: Union[str, bytes]) -> Union[str, bytes]: ...` [http://neopythonic.blogspot.com]:
__( (, ) )
...
//
[ ]
& // / [ `??` ]
?
_ // , , : ._ {...}
!=
[ ()] // () ,
?
&
. // , - shared\,
.()
()
? = () // ``
. =
? = (.) // `.`
= // () , ``
=
, , [+]=( &) // "" : F append, ...
. = .
. {.. = } . =
. =
_()
= 0
(.)
++
() -> & // ( — )
.
() -> &
.
()
.
, :
( Python )
[] ( C++ ).