Skip to content

Instantly share code, notes, and snippets.

@Lewiscowles1986
Last active January 5, 2018 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lewiscowles1986/85b0a5913e44af8dc75c0de7cccc6cb1 to your computer and use it in GitHub Desktop.
Save Lewiscowles1986/85b0a5913e44af8dc75c0de7cccc6cb1 to your computer and use it in GitHub Desktop.
package sample.animals;
import java.lang.IllegalArgumentException;
import java.lang.String;
public class Animal {
private String _name;
private int _legs;
public Animal(String name, int legs) {
_name = name;
_legs = legs;
}
public Animal setLegs(int legs) {
if(legs < 0) {
throw new IllegalArgumentException("Cannot have deficit of legs");
}
return new Animal(_name, legs);
}
public Animal setName(String name) {
if(name.length() < 1) {
throw new IllegalArgumentException("Name must be non-empty");
}
return new Animal(name, _legs);
}
public String getName() {
return _name;
}
public int getLegs() {
return _legs;
}
public String toString() {
return String.format("{%s %d}", _name, _legs);
}
}
package main
import (
"fmt"
)
type Animal struct {
name string
legs int
}
/*
For some reason the signature:
func (animals []Animal) bulkModify (f func(Animal) Animal) []Animal
Doesn't work. This fixes that. #go-nuts on IRC advises against
*/
type Animals []Animal
func (animals Animals) bulkModifyMut(f func(Animal) Animal) Animals {
for i, v := range animals {
animals[i] = f(v)
}
return animals
}
func (animals Animals) bulkModify(f func(Animal) Animal) Animals {
out := make(Animals, len(animals))
copy(out, animals)
return out.bulkModifyMut(f)
}
func manyLegs(animal Animal) Animal {
animal.legs = 999
return animal
}
func main() {
zoo := Animals{Animal{"Dog", 4},
Animal{"Chicken", 2},
Animal{"Snail", 0},
}
fmt.Printf("-> Before update %v\n", zoo)
fmt.Printf(
"-> After update (updated) %v\n",
zoo.bulkModify(manyLegs))
fmt.Printf("-> After update (original) %v\n", zoo)
}
#[derive(Debug)]
struct Animal {
name: String,
legs: u32
}
impl Animal {
pub fn new(name: String, legs: u32) -> Animal {
Animal { name: name,
legs: legs }
}
}
fn main() {
let zoo : [Animal; 3] = [
Animal::new( "Dog".to_string(), 4 ),
Animal::new( "Chicken".to_string(), 2 ),
Animal::new( "Snail".to_string(), 0 ),
];
println!("-> Before update {:?}", zoo);
println!("-> After update (modified) {:?}", zoo.iter()
.map(|animal| {
Animal::new(animal.name.to_string(), 999)
})
.collect::<Vec<Animal>>() );
println!("-> After update (modified) {:?}", zoo);
}
package sample.animals;
import java.util.Arrays;
public final class Application {
public static void main(String[] args) {
Animal[] zoo = {
new Animal("Dog", 4),
new Animal("Chicken", 2),
new Animal("Snail", 0)
};
System.out.printf("-> Before update %s\n", Arrays.toString(zoo));
System.out.printf("-> After update (updated) %s\n", Arrays.toString(
Arrays.stream(zoo).map(animal -> animal.setLegs(999)).toArray()
)
);
System.out.printf("-> After update (original) %s\n", Arrays.toString(zoo));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment