Skip to content

Instantly share code, notes, and snippets.

@tj
Created April 12, 2009 20:37
Show Gist options
  • Save tj/94134 to your computer and use it in GitHub Desktop.
Save tj/94134 to your computer and use it in GitHub Desktop.
// Alternative JavaScript Syntax
Person = :(name, address) { @name!, @address! }
Person::inspect = :{ <: "{@name} lives at {@address}" }
tj := Person('TJ', '314 Bessborough ave')
bob := Person('Bob', 'Some place')
[tj, bob].each(:(person){ print(person.inspect()) })
// C
#include <stdio.h>
#include <string.h>
typedef struct Person {
char *name;
char *address;
} Person;
int
main ()
{
int i;
Person tj = { "TJ", "314 Bessborough ave" };
Person bob = { "Bob", "Some place" };
Person people[2] = { tj, bob };
for (i = 0; i < 2; ++i) printf("%s lives at %s\n", people[i].name, people[i].address);
return 0;
}
-- Haskell
data Person = Person {
personName :: String,
personAddress :: String
}
personInspect :: Person -> String
personInspect p = (personName p) ++ " lives at " ++ (personAddress p)
tj = Person "TJ" "314 bessborough ave"
bob = Person "Bob" "Some place"
people = map personInspect [tj, bob]
main = print (people)
// JavaScript
Person = function(name, address) {
this.name = name
this.address = address
}
Person.prototype.inspect = function() {
return this.name + ' lives at ' + this.address
}
tj = new Person('TJ', '314 Bessborough ave')
bob = new Person('Bob', 'Some place')
each([tj, bob]), function(person){
print(person.inspect())
})
// PHP
<?php
class Person {
public function Person($name, $address) {
$this->name = $name;
$this->address = $address;
}
public function inspect() {
return $this->name . ' lives at ' . $this->address;
}
}
$tj = new Person('TJ', '314 bessborough ave');
$bob = new Person('Bob', 'Some place');
$map = array();
foreach(array($tj, $bob) as $person) {
array_push($map, $person->inspect());
}
print_r($map);
# Ruby
class Person
attr_reader :name, :address
def initialize name, address
@name, @address = name, address
end
def inspect
"#{@name} lives at #{@address}"
end
end
tj = Person.new 'TJ', '314 bessborough ave'
bob = Person.new 'Bob', 'Some place'
people = [tj, bob].map { |person| person.inspect }
p people
@heldrida
Copy link

@SnJedI you answered your own question. This is just a concept to solve a common problem in different languages!

Using Lo-dash (http://lodash.com/docs) you'd have something like:

// JavaScript

Person = function(name, address) {
  this.name = name;
  this.address = address;
};

Person.prototype.inspect = function() {
  return this.name + ' lives at ' + this.address;
};

tj = new Person('TJ', '314 Bessborough ave');
bob = new Person('Bob', 'Some place');

$.each([tj, bob], function(k,obj){
  console.log(obj.inspect());
});

_([tj, bob]).forEach(function(person) {

  console.log(person.inspect()); 

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment