Symta: type person{Name Age} name/Name age/Age person.`<` X = $age < X.age person.as_text = "{$name} ({$age})"
Group = [person{'Bob' 33}, person{'Chris' 16}, person{'Ash' 16}]
say Group.sort.flip
Ruby: class Person attr_reader :name, :age def initialize(name, age) @name, @age = name, age end def <=>(person) # the comparison operator for sorting age <=> person.age end def to_s "#{name} (#{age})" end end
group = [ Person.new("Bob", 33), Person.new("Chris", 16), Person.new("Ash", 23) ]
Symta: qsort@r$[] [H@T] = [@T.keep{?<H}^r H @T.skip{?<H}^r]
Haskell: qsort [] = [] qsort (x:xs) = qsort [y | y <- xs, y < x] ++ [x] ++ qsort [y | y <- xs, y >= x]
Name:
Anonymous2014-12-21 18:46
>>3 J or APL would probably be even shorter, so what?
Name:
Anonymous2014-12-21 19:01
newtype Person = P (String, Int) deriving Eq instance Ord Person where compare (P p1) (P p2) = compare (snd p1) (snd p2) instance Show Person where show (P (name, age)) = show name ++ " (" ++ show age ++ ")"
grp = map P [("Bob", 33), ("Chris", 16), ("Ash", 23)]
Person := Object clone do( name ::= nil age ::= nil curlyBrackets := method( self clone setName(call argAt(0)) \ setAge(call argAt(1))))
p := Person group := List [ p {Bob, 33}, p {Chris, 16}, p {Ash, 23} ]
This way the constructor/DSLs are scoped to the appropriate type. Person could have used squareBrackets as the constructor and it wouldn't interfere with List. You could also have something like this:
Person [ {Bob, 33}, {Chris, 16}, {Ash, 23} ]
without needing to define curlyBrackets on Object by evaluating the arguments in a different context (like Person, for example.)
class Person { read_write_slots: ['name, 'age, 'country] "Creates a new Person instance with the given name, age and country." p = Person new p name: name p age: age p country: country p }
%s=qw/Bob 33 Chris 16 Ash 23/;print"$_ ($s{$_})\n"for sort{$s{$a}<=>$s{$b}}keys%s
Thats why I don't program SHITTY languages like Ruby and Symta.
Name:
Anonymous2014-12-27 1:55
~le failed code tag~
Symta is verbose, compared to Perl.
Perl 5: Modern Perl 5 way use strict; use warnings; use 5.18.0;
{ package Person; use Moose;
use overload '""' => sub { $_[0]->name . ' (' . $_[0]->age . ')' }; has name => is => 'rw', default => sub { 'Anonymous' }; has age => is => 'rw', default => sub { 1337 }; }
my @group = ( Person->new({ name => 'Bob', age => 33 }), Person->new({ name => 'Chris', age => 16 }), Person->new({ name => 'Ash', age => 23 }), );
say for sort { $a->age <=> $b->age } @group;
Perl 5: Blessed way use strict; use warnings; use 5.18.0;
{ package Person; use strict; use warnings;
use overload '""' => sub { "$_[0]->{name} ($_[0]->{age})" };
sub new { my ($class, $args) = @_; bless { name => ($args->{name} // 'Anonymous'), age => ($args->{age} // 0) }, $class; } }
my @group = ( Person->new({ name => 'Bob', age => 33 }), Person->new({ name => 'Chris', age => 16 }), Person->new({ name => 'Ash', age => 23 }), );
say for sort { $a->{age} <=> $b->{age} } @group;
Perl 5: codegolf print "$_->{name} ($_->{age})\n" for sort { $a->{age} <=> $b->{age} } ( { name => 'Bob', age => 33 }, { name => 'Chris', age => 16 }, { name => 'Ash', age => 23 }, );