Real world Common Lisp
1
Name:
Anonymous
2011-03-30 13:42
A simple Stardict client
1 in python
2 , 21 lines with comments. How easy would it be to write an equivalent program in Common Lisp? I think Lisp is not suitable for practical problems.
import re, gzip, struct
idx_file = open('/usr/share/stardict/dic/quick_english-italian.idx', 'r').read()
dic = gzip.open('/usr/share/stardict/dic/quick_english-italian.dict.dz', 'r')
idx_re = r"([\w\W]+?)\x00([\w\W]{8})"
index = {word:struct.unpack('>ii', bytes)
for (word, bytes) in re.findall(idx_re, idx_file)}
def lookup_word(word):
if word not in index:
return False
pos, size = index[word]
dic.seek(pos)
result = dic.read(size)
return result
while True:
result = lookup_word(raw_input("Enter a word: "))
print(result or "Word not found")
_________________
1 - https://github.com/xiangfu/stardict/blob/master/doc/StarDictFileFormat
2 - Requires python 2.7
2
Name:
Anonymous
2011-03-30 13:49
Where are the comments?
3
Name:
Anonymous
2011-03-30 13:51
This implementation looks incredibly efficient
4
Name:
Anonymous
2011-03-30 13:53
I think Lisp is not suitable for practical problems.
It doesn't matter what you think Jabroni.
5
Name:
Anonymous
2011-03-30 13:53
>>1
english-italian
(faggot)
6
Name:
Anonymous
2011-03-30 14:23
>>1
Care to upload those two files? I don't really want to install shit for a stupid
/prog/ ``challenge''.
7
Name:
Anonymous
2011-03-30 14:29
8
Name:
Anonymous
2011-03-30 15:12
(require file/gunzip)
(define-values
(to-aru-dictionary-no-index-file hax-my-dictionary regexp-my-anus)
(values
(open-input-file "tmp222/quick_english-italian.idx")
(let ((out (open-output-bytes)))
(call-with-input-file* "tmp222/quick_english-italian.dict.dz"
(λ (in) (gunzip-through-ports in out)))
(get-output-bytes out))
#px#"([\\w\\W]+?)\0([\\w\\W]{8})"))
(define railgun
(let ((Taro (λ (x) (cons (integer-bytes->integer x #f #t 0 4)
(integer-bytes->integer x #f #t 4 8)))))
(let Christopher ((Xarn '()))
(cond ((regexp-match regexp-my-anus to-aru-dictionary-no-index-file)
=> (λ (Erika)
(Christopher
(cons (cons (bytes->string/latin-1 (cadr Erika))
(Taro (caddr Erika)))
Xarn))))
(else Xarn)))))
(define (|fukou da| misaka)
(cond
((assoc misaka railgun)
=> (λ (sicp)
(bytes->string/latin-1
(subbytes hax-my-dictionary (cadr sicp) (+ (cadr sicp) (cddr sicp))))))))
(|fukou da| "cool")
(|fukou da| "free")
(|fukou da| "ring")
(|fukou da| "tones")
9
Name:
Anonymous
2011-03-30 15:31
>>8
Now seriously,
(require file/gunzip)
(define-values
(dict idx)
(let ((out (open-output-bytes)))
(call-with-input-file* "tmp222/quick_english-italian.dict.dz"
(curryr gunzip-through-ports out))
(values (get-output-bytes out) (open-input-file "tmp222/quick_english-italian.idx"))))
(define (bytes->uint32/BE x s e)
(integer-bytes->integer x #f #t s e))
(define (unpack x)
(cons (bytes->uint32/BE x 0 4)
(bytes->uint32/BE x 4 8)))
(define index
(let loop ((r '()))
(cond ((regexp-match #px#"([\\w\\W]+?)\0([\\w\\W]{8})" idx)
=> (λ (x)
(loop (cons (cons (bytes->string/latin-1 (cadr x))
(unpack (caddr x))) r))))
(else (make-hash r)))))
(define (find word)
(cond
((hash-ref index word #f)
=> (λ (pos+size)
(bytes->string/latin-1
(subbytes dict (car pos+size) (+ (car pos+size) (cdr pos+size))))))))
(find "cool")
(find "free")
(find "ring")
(find "tones")
10
Name:
Anonymous
2011-03-30 15:31
>>8
back to judgimento, uiharu-chan
11
Name:
Anonymous
2011-03-30 20:33
"Real World" CL looks like:
$ cat symcraft/data/units/die/unit.sym
range=666 show="death" say="dead" do=(act "remove" ?)
$ cat symcraft/data/units/human_barracks/unit.sym
layer=2 building=ye size=[3 3]
hp=800 armor=4
cost=["gold"=700 "wood"=450 "time"=200]
trains=["footman" "knight" "ballista"]
$ cat symcraft/data/fireball/unit.sym
layer=3 air=ye impact="explosion" cost=["mana"=100] damage=30 range=8 speed=6
splash=1 bounces=9
$
12
Name:
Anonymous
2011-03-30 20:52
>>11
Is that ``in Lisp''?
13
Name:
Anonymous
2011-03-30 22:14
No. It's ``in Lisp in Lisp DSL''
14
Name:
Anonymous
2011-03-30 22:18
Probably could be made more readable by something like
when in range of 666, show death, saying dead, doing: act "remove" self
But it would obfuscate meaning with vague english words.
15
Name:
Anonymous
2011-03-30 22:32
>>13
We need to go deeper IN MY ANUS
16
Name:
Anonymous
2011-03-31 4:53
>>4
Prove wrong with an actual code. Till then it's just a bullshit talk.
17
Name:
Anonymous
2011-03-31 6:15
>>16
You're under the impression that it matters to anyone what you think.
Hint:
It doesn't.
18
Name:
Anonymous
2011-03-31 6:32
>>17
It's not about me, it's about facts. And it remains a fact until you actually prove it otherwise. With code.
19
Name:
Anonymous
2011-03-31 6:47
Real world Common Lisp (noun )
see Clojure
20
Name:
Anonymous
2011-03-31 6:54
>>19
Clojure is even less ``real world" than Common Lisp. All this
immutable and
lazy crap is anti-practical.
21
Name:
Anonymous
2011-03-31 7:26
>real world
>lisp
lol'd
22
Name:
Anonymous
2011-03-31 7:30
ANII
23
Name:
Anonymous
2011-03-31 7:57
>>19
No.
>>21
U MENA HASKAL
Real world Common Lisp (noun )
see Croma Lisp
25
Name:
test
2011-03-31 8:16
t
26
Name:
Anonymous
2011-03-31 8:18
Isn't laziness only really useful with many expensive code paths? Otherwise it's just wasteful.
27
Name:
Anonymous
2011-03-31 8:22
>>1
It's reasonably simple to implement that in CL, although I would use my own binary file library for parsing the data, oh and using regular expressions to parse binary data? Yuck.
28
Name:
Anonymous
2011-03-31 8:24
29
Name:
Anonymous
2011-03-31 8:34
regular expressions to parse binary data? Yuck.
It's not exactly binary data, it's a list of words with a bit of bytes near them. Binary parts are parsed with struct.unpack.
30
Name:
Anonymous
2011-03-31 9:43
http://docs.python.org/library/struct.html
([\w\W]+?)\x00([\w\W]{8})
'>ii'
@ native native native
= native standard none
< little-endian standard none
big-endian standard none
! network (= big-endian) standard none
Wow! Where is your prized readability now?
31
Name:
Anonymous
2011-03-31 9:52
An example, how this can be done with a macro DSL (unpack)
bytes | unpack order=MSB [a:4 b:4]
32
Name:
Anonymous
2011-03-31 9:59
>>30-31
No one interested in how it
can be done. Show a complete program or your DSL is just a toy.
33
Name:
Anonymous
2011-03-31 10:14
>>31
An example, how this can be done with a macro DSL (unpack)
You don't even need a macro for that stupid unpack shit. A loop, an index, a bytestring, done.
34
Name:
Anonymous
2011-03-31 10:16
>>33
DSL doesn't support loops.
35
Name:
Anonymous
2011-03-31 10:27
>>34
You're a Lisper, you should know that loop = tail-recursion.
36
Name:
Anonymous
2011-03-31 10:30
>>34
for [n [r a]] in (map [? tp.?] resNames |> keep ?,1.ye? |> cnt)
{-> blit n*75+mp.w 0 i:resIcons.r g
-> blitText n*75+mp.w+i.w+4 1 "medium" "$a" g}
37
Name:
Anonymous
2011-03-31 10:30
>>35
CL standard doesn't guarantee tail call elimination.
38
Name:
Anonymous
2011-03-31 10:32
>>37
Serious implementations optimize them.
39
Name:
Anonymous
2011-03-31 10:35
>>38
It's unportable. Who needs tail calls anyway? The
loop macro is much more powerful and it's standard.
40
Name:
Anonymous
2011-03-31 10:41
>>39
loop faggot detected
Newer Posts