Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some more Rosetta Code solutions #1805

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions examples/rosetta/bitmap - bresenham's line algorithm.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
; Bitmap object definition
define :bitmap [
init: method [width :integer height :integer][
\width: width
\height: height
\grid: array.of:@[width height] false
]

setOn: method [x :integer y :integer][
\grid\[y]\[x]: true
]

line: method [x0 :integer y0 :integer x1 :integer y1 :integer][
[dx,dy]: @[abs x1 - x0, abs y1 - y0]
[x,y]: @[x0, y0]
sx: (x0 > x1) ? -> neg 1 -> 1
sy: (y0 > y1) ? -> neg 1 -> 1

switch dx > dy [
err: dx // 2
while [x <> x1][
\setOn x y
if negative? err: <= err - dy ->
[y, err]: @[y + sy, err + dx]

x: x + sx
]
][
err: dy // 2
while [y <> y1][
\setOn x y
if negative? err: <= err - dx ->
[x, err]: @[x + sx, err + dy]
y: y + sy
]
]
\setOn x y
]

string: method [][
join.with:"\n" @[
"+" ++ (repeat "-" \width) ++ "+"
join.with:"\n" map 0..dec \height 'y [
"|" ++ (join.with:"" map 0..dec \width 'x ->
\grid\[dec \height-y]\[x] ? -> "@" -> " "
) ++ "|"
]
"+" ++ (repeat "-" \width) ++ "+"
]
]
]

; Create bitmap
bitmap: to :bitmap @[17 17]!

; and... draw a diamond shape
points: @[
[1 8 8 16]
[8 16 16 8]
[16 8 8 1]
[8 1 1 8]
]

loop points 'p ->
bitmap\line p\0 p\1 p\2 p\3

print bitmap
19 changes: 19 additions & 0 deletions examples/rosetta/bitmap - bresenham's line algorithm.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
+-----------------+
| @ |
| @ @ |
| @ @ |
| @ @ |
| @ @ |
| @ @ |
| @ @ |
| @ @ |
| @ @|
| @ @ |
| @ @ |
| @ @@ |
| @ @ |
| @ @ |
| @ @ |
| @ |
| |
+-----------------+
24 changes: 24 additions & 0 deletions examples/rosetta/inheritance - single.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; Base Animal type
define :animal [
init: constructor [name :string]
]

; Dog type inheriting from Animal
define :dog is :animal [
; ...
]

; Cat type inheriting from Animal
define :cat is :animal [
; ...
]

; Lab type inheriting from Dog
define :lab is :dog [
; ...
]

; Collie type inheriting from Dog
define :collie is :dog [
; ...
]
72 changes: 72 additions & 0 deletions examples/rosetta/rosetta code - find unimplemented tasks.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
;------------------------------------------
; Configuration
;------------------------------------------

API_URL: "https://rosettacode.org/w/api.php"

;------------------------------------------
; Helper Functions
;------------------------------------------

fetchCategory: function [category][
results: []
continue: ""

while ø [
; build query parameters
params: #[
action: "query"
list: "categorymembers"
cmtitle: ~"Category:|category|"
cmlimit: "500"
format: "json"
]

; add continue parameter if we have one
unless empty? continue ->
params\cmcontinue: continue

; perform API request
response: request API_URL params
body: read.json response\body

; extract page titles and add to results
'results ++ map body\query\categorymembers 'page ->
page\title

; check if we need to continue
switch key? body 'continue
-> continue: body\continue\cmcontinue
-> break
]

return results
]

getUnimplementedTasks: function [language][
print "Fetching all programming tasks..."
allTasks: fetchCategory "Programming_Tasks"

print ~"Fetching tasks implemented in |language|..."
languageTasks: fetchCategory language

print "Finding unimplemented tasks..."
return difference allTasks languageTasks
]

;------------------------------------------
; Main Program
;------------------------------------------

if standalone? [
if empty? arg ->
panic "Usage: rosetta-tasks <language>"

language: capitalize arg\0
tasks: getUnimplementedTasks language

print ~"\nFound |size tasks| unimplemented tasks for |language|:\n"

print.lines sort map tasks 'task ->
"• " ++ task
]
Loading