Skip to content

Instantly share code, notes, and snippets.

@suyash
Last active November 16, 2016 03:00
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 suyash/b12566db422791647462 to your computer and use it in GitHub Desktop.
Save suyash/b12566db422791647462 to your computer and use it in GitHub Desktop.
me

me

The me set of programs.

Currently Implemented

  • C
  • C++
  • CoffeeScript
  • Go
  • Java
  • JavaScript
  • JavaScript(ES6)
  • Literate CoffeeScript
  • Objective C
  • Python
  • Ruby
  • Scala
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2016- Suyash Verma <dextrous93@gmail.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
#include <stdio.h>
#include <stdlib.h>
/**
* definitions
*/
typedef struct Person {
char *name;
int age;
} Person;
typedef struct Student {
struct Person person;
char *college;
} Student;
/**
* constructors
*/
Person createPerson (char *name, int age) {
Person person = {name, age};
return person;
}
Student createStudent (char *name, int age, char *college) {
Student student = {createPerson(name, age), college};
return student;
}
/**
* accessors
*/
char* getName (Student *student) {
return student->person.name;
}
int getAge (Student *student) {
return student->person.age;
}
char* getCollege (Student *student) {
return student->college;
}
/**
* Program
*/
int main (int argc, char *argv[]) {
Student me = createStudent("Suyash", 22, "IIT Roorkee");
printf("%s is %d years old and studies at %s.\n", getName(&me), getAge(&me), getCollege(&me));
return 0;
}
#include <iostream>
/**
* @class Person
*/
class Person {
std::string _name;
int _age;
public:
/**
* constructor
*/
Person (std::string name, int age) {
_name = name;
_age = age;
}
/**
* accessors
*/
std::string name () const {
return _name;
}
int age () const {
return _age;
}
/**
* stringify
*/
friend std::ostream& operator << (std::ostream& o, const Person& person) {
o << person.name() << " is " << person.age() << " years old";
return o;
}
};
/**
* @class Student
*/
class Student: public Person {
std::string _college;
public:
/**
* constructor
*/
Student (std::string name, int age, std::string college)
: Person (name, age) {
_college = college;
}
/**
* accessors
*/
std::string college () const {
return _college;
}
/**
* stringify
*/
friend std::ostream& operator << (std::ostream& o, const Student& student) {
operator << (o, static_cast<Person>(student));
o << " and studies at " << student.college() << ".";
return o;
}
};
/**
* Program
*/
int main (int argc, char *argv[]) {
Student me("Suyash", 22, "IIT Roorkee");
std::cout << me << std::endl;
return 0;
}
# https://gist.github.com/reversepanda/5814547
Function::property = (prop, desc) ->
Object.defineProperty @prototype, prop, desc
###
@class Person
###
class Person
constructor: (name, age) ->
@_name = name
@_age = age
@property 'name',
get: -> @_name
@property 'age',
get: -> @_age
# toString
toString: -> "#{@name} is #{@age} years old"
###
@class Student
###
class Student extends Person
constructor: (name, age, college) ->
super name, age
@_college = college
# accessors
@property 'college',
get: -> @_college
# toString
toString: -> "#{super} and studies at #{@college}."
###
code
###
me = new Student "Suyash", 22, "IIT Roorkee"
console.log "#{me}"
package main
import "fmt"
// Person describes an object with a name and an age
type Person struct {
name string
age int
}
func (p Person) String() string {
return fmt.Sprintf("%v is %d years old", p.name, p.age)
}
// Student describes a person who goes to college
type Student struct {
Person
college string
}
func (s Student) String() string {
return fmt.Sprintf("%v and studies at %v.", s.Person, s.college)
}
func main() {
me := Student{Person{"Suyash", 22}, "IIT Roorkee"}
fmt.Println(me)
}
/**
* @class Person
* */
class Person {
/**
* members
* */
private String _name;
private int _age;
/**
* constructor
* */
Person (String name, int age) {
_name = name;
_age = age;
}
/**
* accessors
* */
public String name () {
return _name;
}
public int age () {
return _age;
}
/**
* toString
* */
public String toString () {
return _name + " is " + _age + " years old";
}
}
/**
* @class Student
* */
class Student extends Person {
/**
* members
* */
private String _college;
/**
* constructor
* */
Student (String name, int age, String college) {
super(name, age);
_college = college;
}
/**
* accessors
* */
public String college () {
return _college;
}
/**
* toString
* */
public String toString () {
return super.toString() + " and studies at " + _college;
}
}
/**
* @class Me
* */
public class Me {
public static void main (String args[]) {
Student s = new Student("Suyash", 22, "IIT Roorkee");
System.out.println(s);
}
}
'use strict'
/**
* @class Person
*/
class Person {
constructor (name, age) {
this._name = name
this._age = age
}
/**
* accessors
*/
get name () {
return this._name
}
get age () {
return this._age
}
/**
* toString
*/
toString () {
return `${this.name} is ${this.age} years old`
}
}
/**
* @class Student
*/
class Student extends Person {
constructor (name, age, college) {
super(name, age)
this._college = college
}
/**
* accessors
*/
get college () {
return this._college
}
/**
* toString
*/
toString () {
return `${super.toString()} and studies at ${this.college}.`
}
}
let me = new Student('Suyash', 22, 'IIT Roorkee')
console.log(me.toString())

me (Literate CoffeeScript)

Create a Object.defineProperty wrapper for ES6 classes (taken from https://gist.github.com/reversepanda/5814547)

Function::property = (prop, desc) ->
  Object.defineProperty @prototype, prop, desc

Create a class Person that describes a generic person with a name and age, with appropriate accessor methods.

class Person
  constructor: (name, age) ->
    @_name = name
    @_age = age

  @property 'name',
    get: -> @_name

  @property 'age',
    get: -> @_age

  toString: -> "#{@name} is #{@age} years old"

Then a class Student that describes a person that goes to college.

class Student extends Person
  constructor: (name, age, college) ->
    super name, age
    @_college = college

  @property 'college',
    get: -> @_college

  toString: -> "#{super} and studies at #{@college}."

Then, create me

me = new Student "Suyash", 22, "IIT Roorkee"

and print its info

console.log "#{me}"
#import <Foundation/Foundation.h>
/**
* @interface Person
* */
@interface Person: NSObject
@property NSString* name;
@property NSNumber* age;
- (id)initWithName: (NSString*)name age: (NSNumber*)age;
- (NSString*)description;
@end
/**
* @implementation Person
* */
@implementation Person
- (id)initWithName: (NSString*)name age: (NSNumber*)age {
self = [super init];
if (self) {
self.name = [name copy];
self.age = [age copy];
}
return self;
}
- (id)init {
return [self initWithName: @"" age: @-1];
}
- (NSString*)description {
return [NSString stringWithFormat:@"%@ is %@ years old", self.name, self.age];
}
@end
/**
* @interface Student
* */
@interface Student: Person
@property NSString* college;
- (id)initWithName: (NSString*)name age: (NSNumber*)age college: (NSString*)college;
- (NSString*)description;
@end
/**
* @implementation Student
* */
@implementation Student
- (id)initWithName: (NSString*)name age: (NSNumber*)age college: (NSString*)college {
self = [super initWithName:name age:age];
if (self) {
self.college = [college copy];
}
return self;
}
- (id)init {
return [self initWithName: @"" age: @-1 college: @""];
}
- (NSString*)description {
return [NSString stringWithFormat:@"%@ and studies at %@", [super description], self.college];
}
@end
/**
* main method
* */
int main() {
@autoreleasepool {
Person* p = [[Student alloc] initWithName: @"Suyash" age: @23 college: @"IIT Roorkee"];
NSLog(@"%@", p);
}
}
/**
* http://javascript.crockford.com/prototypal.html
* http://ejohn.org/blog/javascript-getters-and-setters/
*/
/**
* Polyfill Object.create
*/
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F () {}
F.prototype = o
return new F()
}
}
/**
* @function extend
* extends child with parent
* and adds immutable 'super' property
*/
function extend (child, parent) {
if (typeof child !== 'function' || typeof parent !== 'function') {
return
}
child.prototype = Object.create(parent.prototype)
child.prototype.constructor = child
child.prototype.__defineGetter__('super', function () {
return parent
})
return child
}
/**
* @function _super
* calls super class method for an object
*/
function _super (obj, property, args) {
if (typeof obj !== 'object' ||
typeof property !== 'string' ||
(typeof args !== 'undefined' && args.constructor !== Array)) {
return
}
return obj.super.prototype[property].apply(obj, args)
}
/**
* @constructor Person
*/
function Person (name, age) {
this.__defineGetter__('name', function () {
return name
})
this.__defineGetter__('age', function () {
return age
})
}
Person.prototype.toString = function () {
return this.name + ' is ' + this.age + ' years old'
}
/**
* @constructor Student
*/
function Student (name, age, college) {
this.super(name, age)
this.__defineGetter__('college', function () {
return college
})
}
extend(Student, Person)
Student.prototype.toString = function () {
return _super(this, 'toString') + ' and studies at ' + this.college + '.'
}
/**
* me
*/
var me = new Student('Suyash', 22, 'IIT Roorkee')
console.log(me.toString())
class Person(object):
"""
defines a generic person properties
"""
def __init__ (self, name, age):
"""
instantiate a person with a name and an age
"""
self._name = name
self._age = age
@property
def name (self):
"""
access the name of the person
"""
return self._name
@property
def age (self):
"""
access the age of the person
"""
return self._age
def __unicode__ (self):
"""
give a string representation of the object
"""
return "%s is %d years old" % (self.name, self.age)
class Student(Person):
"""
defines a student as a person who goes to college
"""
def __init__ (self, name, age, college):
"""
instantiate a student with a name, an age and the colleges he attends
"""
super(self.__class__, self).__init__(name, age)
self._college = college
@property
def college (self):
"""
access the college the student attends
"""
return self._college
def __unicode__ (self):
"""
give a string representation of the student
"""
return "%s and studies at %s" % (super(self.__class__, self).__unicode__(), self.college)
def main ():
me = Student("Suyash", 22, "IIT Roorkee")
print unicode(me)
if (__name__ == "__main__"):
main()
# creates a new Person
make.Person <- function (name, age) {
function () {
paste(name ,"is", age, "years old")
}
}
# creates a new student
make.Student <- function (name, age, college) {
person <- make.Person(name, age)
function () {
paste(person(), "and studies at", college)
}
}
# create me
me <- make.Student("Suyash", 22, "IIT Roorkee")
me()
# class Person
class Person
attr_reader :name, :age
def initialize (name, age)
@name = name
@age = age
end
# to_s
def to_s
"#{name} is #{age} years old"
end
end
# class Student
class Student < Person
attr_reader :college
def initialize (name, age, college)
super(name, age)
@college = college
end
# to_s
def to_s
"#{super} and studies at #{college}."
end
end
me = Student.new "Suyash", 22, "IIT Roorkee"
puts me
/**
* @class Person
* */
class Person(name: String, age: Int) {
/**
* accessors
* */
def getName = name
def getAge = age
/**
* toString
* */
override def toString = name + " is " + age + " years old"
}
/**
* @class Student
* */
class Student(name: String, age: Int, college: String) extends Person(name: String, age: Int) {
/**
* accessors
* */
def getCollege = college
/**
* toString
* */
override def toString = super.toString() + " and studies at " + college + "."
}
/**
* @object me
* */
object me {
def main (args: Array[String]) = {
val me = new Student("Suyash", 22, "IIT Roorkee")
println(me)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment