Skip to content

Commit

Permalink
Merge pull request #73 from jeremydouglass/R
Browse files Browse the repository at this point in the history
add Processing.R (R language mode) examples
  • Loading branch information
jeremydouglass authored Apr 18, 2020
2 parents 3f1e96c + 17afb36 commit 1f7403c
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 4 deletions.
33 changes: 31 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ Several options for contributing are listed below.

To identify an existing Processing task already on the Rosetta Code wiki that is not yet part of this example set:

1. on the Rosetta Code wiki, browse [Processing tasks](https://rosettacode.org/wiki/Category:Processing) or [Python mode tasks](https://rosettacode.org/wiki/Category:Processing_Python_mode).
1. on the Rosetta Code wiki, browse tasks for:
- [Processing tasks](https://rosettacode.org/wiki/Category:Processing), or
- [Python mode tasks](https://rosettacode.org/wiki/Category:Processing_Python_mode), or
- [Processing.R tasks](https://rosettacode.org/wiki/Category:Processing.R)
If you find a task that is not listed in this repos `examples` folder:
2. [create a new issue](https://github.com/jeremydouglass/rosetta_examples_p5/issues) named for that task


### Contribute a new task to the Rosetta Code wiki

1. on the Rosetta Code wiki, browse [Tasks not implemented in Processing](https://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_Processing) or [Tasks not implemented in Python mode](https://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_Processing_Python_mode).
1. on the Rosetta Code wiki, browse unimplemented tasks:
- [Tasks not implemented in Processing](https://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_Processing), or
- [Tasks not implemented in Python mode](https://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_Processing_Python_mode), or
- [Tasks not implemented in Processing.R](https://rosettacode.org/mw/index.php?title=Reports:Tasks_not_implemented_in_Processing.R&action=edit&redlink=1)
2. select a task to add
3. edit that page (full page edit)
4. add a Processing section -- alphabetically using header markup
Expand All @@ -40,6 +46,29 @@ The result may look like this:
</lang>
```

or this:

```
==={{header|Processing Python mode}}===
<lang python>def draw():
background(0)
ellipse(width/2, height/2, mouseX, mouseX)
println(frameCount)
</lang>
```

or this:

```
==={{header|Processing.R}}===
<lang r>draw <- function() {
background(0)
ellipse(width/2, height/2, mouseX, mouseX)
stdout$println(frameCount)
}
</lang>
```

Optionally, a single entry may contain plain text descriptions and multiple code blocks, if needed--for example, if providing two separate solutions to the same problem (simple and complex, or demonstrating two different approaches). Code may also be accompanied by example output, headed by {{out}} and wrapped in `<pre>` tags.

```
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

Examples from [Rosetta Code](http://rosettacode.org/)
for [Processing](http://processing.org) (Java Mode)
and [Python Mode for Processing](https://py.processing.org/).
and [Python Mode for Processing](https://py.processing.org/)
and [Processing.R](https://processing-r.github.io/).


These sets of example Processing sketches (both Java mode and Python mode) compile the available versions of code tasks listed on the Rosetta Code
These sets of example Processing sketches (includiing Java mode, Python mode,
and R mode) compile available versions of code tasks listed on the Rosetta Code
wiki. The examples are directly downloadable from the repository. They may
also be loaded directly into the Processing PDE desktop environment through
Contributions Manager under the Examples tab.
Expand Down Expand Up @@ -54,6 +56,8 @@ set. All sketches will be installed in the Examples subfolder of your
Processing data folder, and available through the Examples pop-up menu under
Contributed Examples > Rosetta Examples for P5.

Examples are organized in subfolders by mode / language: Java, Python, and R.


## License and Contributions

Expand Down
52 changes: 52 additions & 0 deletions examples/R/AckermannFunction/AckermannFunction.rpde
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Ackermann function
# https://rosettacode.org/wiki/Ackermann_function#Processing.R
# Processing 3.4
# 2009-06-30 ShinTakezou (R)
# 2020-04-17 Jeremy Douglass
#
# The Ackermann function is a classic example of a recursive function, notable
# especially because it is not a primitive recursive function. It grows very
# quickly in value, as does the size of its call tree.
#
# The Ackermann function is usually defined as follows:
#
# ( n+1 if m = 0
# A(m,n) = ( A(m-1, 1) if m > 0 and n = 0
# ( A(m-1, A(m, n-1)) if m > 0 and n > 0
# Its arguments are never negative and it always terminates.
#
# Task:
#
# Write a function which returns the value of A(m,n).
# Arbitrary precision is preferred (since the function grows so quickly),
# but not required.

setup <- function() {
for (m in 0:3) {
for (n in 0:4) {
stdout$print(paste(ackermann(m, n), " "))
}
stdout$println("")
}
}

ackermann <- function(m, n) {
if ( m == 0 ) {
return(n+1)
} else if ( n == 0 ) {
ackermann(m-1, 1)
} else {
ackermann(m-1, ackermann(m, n-1))
}
}

# Output:
#
# 1 2 3 4 5
# 2 3 4 5 6
# 3 5 7 9 11
# 5 13 29 61 125

# note that Processing.R exceeds its stack depth at ~ n==6
# and returns null.
# -- see also https://rosettacode.org/wiki/Find_limit_of_recursion#R
47 changes: 47 additions & 0 deletions examples/R/NinetyNineBottles/NinetyNineBottles.rpde
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# NinetyNineBottles
# https://rosettacode.org/wiki/99_Bottles_of_Beer#Processing.R
# Processing 3.4
# 2020-04-17 Jeremy Douglass
#
# Task:
#
# Display the complete lyrics for the song:
# 99 Bottles of Beer on the Wall.
#
# The lyrics follow this form:
#
# 99 bottles of beer on the wall
# 99 bottles of beer
# Take one down, pass it around
# 98 bottles of beer on the wall
#
# 98 bottles of beer on the wall
# 98 bottles of beer
# Take one down, pass it around
# 97 bottles of beer on the wall
#
# ... and so on, until reaching 0.
#
# Grammatical support for "1 bottle of beer" is optional.
#
# As with any puzzle, try to do it in as creative/concise/comical
# a way as possible (simple, obvious solutions allowed, too).


# immediately prints all output to the console
setup <- function() {
stdout$print(bottlesong(99))
}

bottlesong <- function(num) {
verses = ""
for(i in num:1){
verses = paste0(verses,
num," bottles of beer on the wall \n",
num," bottles of beer \n",
"Take one down, pass it around \n",
num-1, " bottles of beer on the wall \n\n", sep="");
num <- num - 1
}
return(verses)
}
36 changes: 36 additions & 0 deletions examples/R/OneHundredDoors/OneHundredDoors.rpde
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 100 Doors
# http://rosettacode.org/wiki/100_doors#Processing.R
# Processing 3.4
# 2008-09-16 Gregg Lind (R)
# 2019-12-10 Jeremy Douglass
#
# There are 100 doors in a row that are all initially closed.
# You make 100 passes by the doors.
#
# The first time through, visit every door and toggle the door
# (if the door is closed, open it; if it is open, close it).
# The second time, only visit every 2nd door (door #2, #4, #6, ...),
# and toggle it.
# The third time, visit every 3rd door (door #3, #6, #9, ...),
# etc, until you only visit the 100th door.
#
# Task:
#
# Answer the question:
# What state are the doors in after the last pass?
# Which are open, which are closed?

setup <- function() {
for(door in doors(100, 100)) {
stdout$print(paste(door, ""))
}
}

doors <- function(ndoors=100,passes=100) {
doors <- rep(FALSE,ndoors)
for (ii in seq(1,passes)) {
mask <- seq(0,ndoors,ii)
doors[mask] <- !doors[mask]
}
return (which(doors == TRUE))
}

0 comments on commit 1f7403c

Please sign in to comment.