-
-
Save takikawa/d55480f2f83a22d101c4 to your computer and use it in GitHub Desktop.
Translations of Pharo examples from https://medium.com/@svenvc/elegant-pharo-code-bb590f0856d0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; 1. Get the HTML source of a web page | |
(require net/url) | |
(port->string (get-pure-port (string->url "http://www.racket-lang.org"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; 3. Decimal digit length of 42! | |
(require math) | |
(string-length (number->string (factorial 42))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang web-server/insta | |
;; 4. Set up an HTTP server that returns the current timestamp | |
(require racket/date) | |
(define (start req) | |
(response/output | |
(λ (op) (display (date->string (current-date)) op)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; 5. Split a string on dashes, reverse the order of the elements and join them using slashes | |
(string-join (reverse (string-split "1969-07-20" "-")) "/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; 6. Convert all JPG files in the current directory to PNG format | |
(require racket/draw) | |
(for ([file (directory-list (current-directory))] | |
#:when (file-exists? file) | |
#:when (equal? (filename-extension file) #"jpg")) | |
(call-with-input-file file | |
(λ (in) | |
(send (read-bitmap in) save-file (path-replace-suffix file ".png") 'png)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; 7. Sum of the first 64 primes | |
(require math) | |
(sum (next-primes 0 64)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; 8. Extract a Unix format timestamp from the 5th to 8th byte of a byte array given in hex | |
(require file/sha1) | |
(seconds->date | |
(integer-bytes->integer | |
(hex-string->bytes "CAFEBABE4422334400FF") | |
#t #t 4 8)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/gui | |
;; 9. Show a PNG image retrieved from a URL | |
;; Note: run in DrRacket | |
(require net/url) | |
(call/input-url (string->url "http://www.racket-lang.org/logo-and-text.png") | |
get-pure-port | |
(λ (in) (read-bitmap in 'png))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; 10. Verify that a fraction is close to ∏ | |
;; Note: not quite the same, but similar | |
(require math) | |
(< (absolute-error (/ 355 113) pi) 0.00001) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; 11. Test whether one set is included in another one | |
(subset? (set 'f 'd 'b) (set 'a 'b 'c 'd 'e 'f)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; 13. Average of the prime factors of 2^32 — 1 | |
(require math) | |
(mean (prime-divisors (sub1 (expt 2 32)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require math) | |
(stddev '(2 4 4 4 5 5 7 9) #:bias #t) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; 18. Save the HTML source of a web page to a file | |
(require net/url) | |
(copy-port (get-pure-port (string->url "http://www.racket-lang.org")) | |
(open-output-file "page.html")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; 19. Basic grep on a file using a regular expression | |
(for/list ([line (port->lines (open-input-file "/tmp/foo.txt"))] | |
#:when (regexp-match? #rx"^.*.jpg" line)) | |
line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment