[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Ruby artisans on suicide watch
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 120
Thread images: 5
File: better-than-x-8-728.jpg (66 KB, 942x728) Image search: [Google]
better-than-x-8-728.jpg
66 KB, 942x728
Ruby artisans on suicide watch
>>
>interpreted vs compiled language
>hurr why use wood instead of sand
>>
>whos more pretensious?

"Haskellers" wins yet again!
>>
hurr durr beautiful compact code
spend minutes verifying a single line
>>
>over 80 characters long
Absolutely disgusting
>>
but anon, that one line of haskell is way harder to read and write and doesn't look as good
>>
>>54804732
import qsort;
qsort.sort(fuck_you)

python wins again
>>
>>54804732
>lets make variable names shorter and non-descriptive, and stuff everything into a single line
>so terse
>so beautiful
>>
>>54805047
x:xs is a pretty common Haskell naming convention that gets the point across.

It probably went over your webdev-tier brain desu senpaialam ding dong.
>>
File: 1460066248490.png (260 KB, 600x800) Image search: [Google]
1460066248490.png
260 KB, 600x800
>>54805124
>x:xs is a pretty common Haskell naming convention that gets the point across.
irrelevant. the names are still less descriptive and readable that in the upper section
>webdev
not really and I even like Ruby and hipsters programming in it are obnoxious, but Haskell fags are the worst
>>
>>54805253
If you've never programmed in the language, sure. I don't see that as much of an issue since it's supposed to be generic, and x is a generic enough name. (x:xs) already tells you that it's a list. Less is more in this case.

Then again you could use list as the name alongside the head/tail functions but I'd argue that would make it less readable.

I agree on that comment about Ruby/Haskellfags though.
>>
>let's compare two entirely different programming paradigms based textually
sounds good OP
>>
>>54804978
> ;
>>
>>54804732
If you want a practical and pragmatic functional language, use Clojure.

Haskell is for autistic nerds who value purity and arcane (if aesthetic) syntax over anything usable.

Ruby is for light scripting as was never meant to become a massive dependency-laden mess used in major parts of the web's infrastructure.
>>
>>54804732
I don't know about the Ruby one, but the Haskell code is not true QuickSort, it's asymptotically quadratic in best case.
>>
>>54804732
One is readable and the other is unnecessarily compact.
>>
let rec qsort cmp = function
| [] -> []
| x :: xs ->
let rec loop lower greater = function
| [] -> lower, greater
| y :: ys ->
let lower, greater =
if cmp x y < 0 then
lower, y :: greater
else
y :: lower, greater in
loop lower greater ys in
let lower, greater = loop [] [] xs in
let greater = qsort cmp greater in
let lower = qsort (fun a b -> - cmp a b) lower in
List.rev_append lower (x :: greater)
;;

let print l =
List.iter (fun n -> Printf.printf "%d " n) l;
print_newline ()
;;

let test l =
print_endline "======================================================\
==================";
print l;
let l = qsort ( - ) l in
print l
;;

let mk_list n =
let rec loop accu = function
| 0 -> accu
| n -> loop (Random.int 90 + 10 :: accu) (pred n) in
loop [] n
;;

let main () =
Random.self_init ();
test [];
test [42];
test [3; 1; 4; 1; 5];
test (mk_list 20);
test (mk_list 20);
0
;;

let () = exit (main ());;
>>
>>54806774
let qsort cmp l =
let rec qsort accu = function
| [] -> accu
| x :: xs ->
let rec loop lower greater = function
| [] -> lower, greater
| y :: ys ->
let lower, greater =
if cmp x y <= 0 then
lower, y :: greater
else
y :: lower, greater in
loop lower greater ys in
let lower, greater = loop [] [] xs in
let accu = qsort accu greater in
let accu = x :: accu in
qsort accu lower in
qsort [] l
;;

Faster version with a stable sort.
>>
1. The Ruby code appears to be more readable.
2.
list.sort!


In any case, Haskell and Ruby aren't exactly comparable languages. Haskell is statically typed and compiled, Ruby is dynamically typed and interpreted. Haskell is functional, Ruby is multi-paradigm with a heavy emphasis on smalltalk-style object orientation.
>>
>>54805607
> unnecessarily compact
That is the best way to do it. It's literally saying:

> Put the first item in the middle and quicksort the things less than it onto the left, and quicksort the things greater than it on the right.
>>
>>54807023
> list.sort!
Now quicksort the days of the week with the same function.
>>
>>54806774
>>54806909
OCaml or F#? Hell, it might work in both.
>>
>>54807141
>F#
Absolutely disgusting. I have taste. Why would I use a slower and less safe language? I am not stupid.
>>
>>54804978
literally 100% victory

ruby hipsters and haskell neckbeards on suicide watch
>>
why even compare them. haskell is still a nightmare to integrate with postgres and even serve up a simple json api.

what we really need is a high level functional language with a killer webdev library. rails is already old shit, but the new shit like meteor is terrible. what the hell are you all doing CS people? us web devs aren't smart enough to alleviate our own condition
>>
>>54804978
To all practical matters this
>>
>>54804851

This. Haskell is an esoteric programming language by my book.
>>
>>54807064

Define the <=> operator for your "day of the week" class. Done. Alternatively, use
list.sort_by! { |a,b|
# Your method of comparing days of weeks here
}
>>
>>54804732

>haskell wins

Oh god, you really have some issues, man.
Every langauge has pros and cons, for example, I'd like to see this in Haskell:


# put http article in text file

require "open-uri"

remote_data = open("http://en.wikipedia.org/wiki/Ada_Lovelace").read
File.open("my-downloaded-page.html", "w") { |f|
f.write(remote_data) }



>>54805553

>If you want a practical and pragmatic functional language, use Clojure.

If you really want to to leverage a functional langauge, you can just use Elixir, which has a syntax almost identical to Ruby (it was created by a ruby guy) and runs the Erlang Virtual Machine, arguably the best language for massive concurrent applications.

Also languages like Scala or Swift took many of Ruby's concepts (I wonder why??)..
Good language design is good language design, period.
>>
>>54807373
toppest
>>
>>54804732
>x:xs
That's fucking stupid though, and would take quadratic time on an already-sorted list.

Of course, you gloss over that in attempt to prove how superior Haskell is.
>>
>>54807406
>would take quadratic time on an already-sorted list
1) You seriously don't know how complexity work in Haskell.
2) quick sort has quadratic complexity, Haskell is not guilty.
>>
>the fewer number of lines it needs to implement quicksort, the better the language is

Said no one ever.
>>
>>54807398
>require "open-uri"
Just use curl.
>>
>>54807390
data Weekday = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving Ord

quicksort [Friday, Tuesday, Monday]
>>
>>54804732
I don't like Ruby at all, I'm a C# developer for a living, and I don't know Haskell at all

The Ruby code is at least intuitive. I can understand what is going on without knowing Ruby's specific syntax

The Haskell code on the other hand just looks fucking alien to me.

qsort is an array and is initialized to an array (I think) ?

Then qsort is now a delegate function all of the sudden? What is filter, what is being incremented with the ++?

All of these questions probably have simple answers but my point is it isn't intuitive at all.

"Beautiful" code is subjective, I suppose.
>>
>>54807665
As someone who knows no C at all, and mostly Lisp/Haskell/Clojure, the Haskell just 'looks' right. I can't describe how, I guess I haven't been brought up in the imperative mould.
>>
>>54807665
PS. qsort is a recursive function, filter is also a function. The list given to qsort is destructured into its first element 'x' and the rest of the list 'xs', allowing qsort to work on it element-wise.
>>
>>54807665
It's defined like it would be in a logic programming language:
qsort of an empty list is an empty list
qsort o a list of at least one element (first element bound to x, the rest of the list bound to xs) is
- qsort applied to the list obtained by filtering elements not < x from xs
- x
- qsort applied to the list obtained by filtering elemtns not >= x from xs
(contatenated in that order)

I don't know haskell but I know scheme and a little logic programming.
>>
>>54807605

Without defining a new type, the equivalent in Ruby might be something like this:

Weekday = { sunday: 0, monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6 }
[:friday, :tuesday, :monday].sort { |x,y| Weekday[x] <=> Weekday[y] }
>>
>>54807686
He said C#. Not C.
>>
>>54807718
> qsort o a list of at least one element
qsort of a list of at least two elements.

A list of one element would be
[x]
>>
>>54807743
... wat

How do you expect any enterprise-scale web application to function without a server-side language

Where would you put the business logic? Would you not use an ORM?

just... wat.
>>
>>54807760
Does it not work like lists in scheme where
(first '(a))
=> a
(rest '(a))
=> '()

?
(using first instead of car and rest instead of cdr for readability)
>>
>>54807774
erm, I guess it was a troll.

>>54807760
>>54807718

I guess its just a different kind of programming I'm not used to, then.

I got about 1/3rd of the way through SICP a couple years ago and lost interest because... well I just didn't see any practical applications of that particular language

Are these languages (scheme, haskell) used anywhere besides leisure programming and AI ?
>>
>>54807789
(just in case it needs clarification:
(first '(a b c))
=> a
(rest '(a b c))
=> '(b c)

)
>>
>>54807789
You can do that, but not in destructuring pattern matching.

Pattern matching on (1) an empty list, (2) a list of one element, (3) a list of at least two elements:
fn [] = ...
fn [x] = ...
fn (x:xs) = ...


You can pattern match as many elements as you like this way.

If you want to use 'head' or 'rest', you can use guards:
fn list
| (head list) == x = ...
| (head (tail list) == y = ...
| otherwise = ...
>>
>>54807859
So then how does the code in OP behave when there is one element in the list? Shouldn't xs pattern match to an empty list?
>>
>>54807806
> Are these languages (scheme, haskell) used anywhere besides leisure programming and AI ?
I have no idea, I'm a chemist who does Haskell/Lisp/etc. for fun.
>>
>>54807806
They're used all over the place by people who like them. idk if there's enterprise sort of jobs available. More the sort of jobs where your boss doesn't care what tool you use to get the job done.
>>
>>54807885
>>54807912
So then why is there even a debate between Ruby and Haskell?

They aren't even used in the same way. I don't understand this thread at all.

I'm leaving now.
>>
>>54807876
> Shouldn't xs pattern match to an empty list?
Yes, indeed it does.

In Haskell, lists are usually written as
[1,2,3,4,5]


But this is just syntactic sugar for how it is really interpreted:
1 : 2 : 3 : 4 : 5 : []

The ':' is the 'cons' from stuff like Lisp, and every list terminates with an empty list, so a list of one element is
1 : []


So in the OP's code, the pattern '(x:xs)' matches, for example, the '1 : []'.
>>
>>54804732

You could write a Ruby version which looks a little bit more like the Haskell version:

(I didn't test this, so..)

def qsort(xs)
return xs if xs.size <= 1
x = list.shift; return qsort(xs.select(|y| y < x)) + x + qsort(xs.select(|y| y >= x))
end
>>
>>54807398
>I'd like to see this in Haskell
Not OP but here:

import Network.Wreq
import Control.Lens
import qualified Data.ByteString.Lazy as BS

main = do
response <- get "http://en.wikipedia.org/wiki/Ada_Lovelace"
BS.writeFile "my-downloaded-page.html" $ response ^. responseBody
>>
>>54804732
>terse and beautiful code
>not Lisp
Ayy
>>
>>54807996

Looks nice!
A lot of libraries though..

>>54807960

Aaa, I fucked up..

def qsort(list)
return list if list.size <= 1
x = list.shift
qsort(list.select{|y| y < x} ) + x + qsort(list.select{|y| y >= x} )
end


As you see, this version is pretty close to the haskell version:

1. if one element, return
2. get the first element (in haskell it's implicitly done with "x:xs")
3. return the group smaller + pivot element + the group bigger


So at the end of the day, Haskell is a great language and Ruby is, so why am I writing at this stupid cambodian motorcycle board again..?
>>
>>54807930
There shouldn't be. Language wars are idiotic and only perpetrated by trolls and those who don't understand what they're saying.
>>
>>54804851
I don't know Haskell and I can read that line in 15 sec
>>
>>54807859
Not true, [] is not technically an element, it's the terminating constructor. (x:xs) matches any list of one element or more. [x] matches a list of only one element.
>>
>>54805253
I am all for descriptive variables.
In this example there is no reason for them though. There is no need for pivot, no left or right.
No description necessary since the structure in that line shows what happens. Just work with the list and x:xs is perfect for that.
>ruby btfo
>>
>>54807665
>(++) Appends two lists
Then it's a lot more clear what's going on.
>>
>>54807760
[1] == [1] ++ [] == 1:[]

>>54807789
Yes.
>>54807885
Facebook uses Haskell and read about some finance company using it.
>>
>>54807398
>Scala took many of Ruby's concepts
u wot m8?
Scala is basically haskell on jvm
elixir is good tho, gonna learn it in a couple of months probably. Clojure's where it's at imo.
>>
>>54804851
>I don't understand the syntax of something I've never learned
do shitposters shitpost
>>
>>54805253
>functionnamethatisverylongbecauseImretarded (theheadofthelist:therestofthelist)
>>
>>54804732
lmao lets see the Makefile
>>
>>54808832
Elixir is pretty neat, fast as fuck. So is Clojure.
>>
Quicksort is in-place and swaps elements of the array.
>>
>>54804732
someone do a clojure version
>>
>>54807188
because ocaml still can't operator overload
>>
>>54807442
not when it's shorter lines, easier to understand and cleaner
>>
>>54807423
>1) You seriously don't know how complexity work in Haskell.
I don't think I've heard of anything stupider. Complexity isn't something programming languages can magically be immune to.

>2) quick sort has quadratic complexity, Haskell is not guilty.
Only in the worst-case, e.g. when you're sorting an already-sorted list, which your stupid fucking code snippet wouldn't know how to handle.
>>
>>54805553
>massive dependency-laden mess used in major parts of the web's infrastructure.
I agree. "require" is the worst fucking thing about Ruby.
>>
>>54809217
he didn't day it didn't have time complexity, he said you don't know how it works
>>
>>54805253
>reddit

Fuck off
u
c
k

o
f
f
>>
>>54804732
I don't give a flying fuck how short the fucking source code is. Programming languages do not exist to exploit their syntaxes to write obscure condensed code that needs to be read ten times to understand. They exist to be human readable. As long as it produces machine code of equivalent performance, the code can be as long and descriptive as it needs to be. I don't need to impress my nerd friends with obscure code.
>>
File: 1461386922706.png (321 KB, 480x650) Image search: [Google]
1461386922706.png
321 KB, 480x650
>>54809339
>he can't understand the code from looking at it for a few seconds
I bet you think all code is supposed to be understood at sight
Its understandable, I mean, the only code you look at is fizzbuzz.
>>
>>54807398
>Swift took many of Ruby's concepts
which? it was very much inspired by Rust and Haskell (for example, protocols vs. traits vs. type classes)
>>
>>54804732
>that monstrous sequence of characters
>beautiful

Shit taste taken to a pathological level.
>>
>>54804732
Clojure

(defn quick-sort
[lst pred]
(if (empty? lst)
[]
(let [car (first lst)
cdr (rest lst)
cmp (fn [x] (pred car x))
left (quick-sort (filter cmp cdr) pred)
right (quick-sort (filter (complement cmp) cdr) pred)]
(concat left [car] right))))
>>
>>54809617
ex.
=> (quick-sort [2 3 89 9 1] >)
(1 2 3 9 89)
>>
>>54804732
>beautiful
missed that part.
>>
>>54809645
he wrote it like shit
here's a better version
 
import Data.List
qsort [] = []
qsort (x:xs) = s ++ [x] ++ b
where (s, b) = partition (<x) xs
>>
>>54809339
This code is very straightforward though
>>
>>54810249
Even better
sorted(listy)
>>
>>54810753
not quicksort
just mergesort
also
>()
>>
Java masterrace reporting in
>>
>>54804732

> you can do retarded 1 liners that make no sense

if obfuscation = good C wins
>>
>>54810753
sort list


checkmate mommy
>>
>>54810855
It's timsort
>>
>>54804978
[3,5,9,1,6,2,8,4,7].sort()
>>
>>54809279
How does your stupid haskell oneliner avoid sorting in O(n^2) when processing a sorted list, then? If you're going to call me out on my (correct) assertion, at least bring some facts.
>>
>>54810249
cool
let rec qsort = function
| [] -> []
| x::xs ->
let s,l = xs |> List.partition ((>=) x)
qsort s @ [x] @ qsort l
>>
>>54812298
Here's a Clojure version based on this, not quite as nice given Clojure's incompatibilities between collection types.

(defn qsort [coll]
(let [x (first coll)
xs (rest coll)]
(condp = [x xs]
[nil '()] []
(let [[l s] ((juxt filter remove) #(>= % x) xs)]
(vec (concat (qsort s) [x] (qsort l)))))))
>>
that isn't even quicksort.
>>
>>54804978
This is the problem with Python skript kidZ, they learn to "just use the existing library"
>>
>>54808832
Haskell and Scala are not even remotely similar. Completely different syntax, semantics, data modeling, everything, frankly.
>>
>rewriting 50 year old algorithms by hand

freetards
>>
>>54813438
>implement quicksort
>>rewriting 50 year old algorithms by hand

Are you retarded?
>>
>>54813466
no, I don't use haskell

seriously though, there is literally no point in implementing ancient shit that should be available through libraries
>>
>>54813489
Are you a fan of Python, by any chance?
>>
>>54813537
not really, forced indentation is for cucks
>>
>>54813555
Java or .NET?
>>
>>54813607
the latter, java is for pajeets
>>
>>54813682
Same kind of cancer.
>>
>>54813695
*tips fizzbuzz*
>>
>>54804732
>study CS
>first semester has me learning sorting algorithms
>prof spends half a fucking lecture explaining quicksort
>i understand it, then proceed to do other shit and forget about sorting algorithms
>i just see this piece of ruby code
>don't even know ruby very well anymore, guess the "partition" function
>understand the algorithm instantly
>holy shit this is so much easier to understand when seeing the implementation than when hearing the explanation

also, i like haskell, but claiming this one line of haskell is better than the ruby implementation can only be made by people who have never worked on bigger projects with other people. dude, i don't want to spend 5 minutes figuring out what exactly your code does, nor do i want to debug that shit. also, it's more than 80 chars long. into the trash it goes, instantly, being replaced by something that is readable and thus maintainable.
>>
>>54807064
>>54807390
>>54807605
>>54807728
not entirely sure, but i think if you just request the time module weekdays are already in an ordered namespace.
>>
>>54813807

qsort [] = []
qsort (x:xs) = qsort (filter (<x) xs) ++ [x] ++ qsort (filter (>= x) xs)


if the input to qsort is [] (an empty list), return [] (an empty list - not necessarily the same element type)

qsort (x:xs)
if the input to qsort fits x:xs (a list with at least 1 element - x - xs being the rest of the list (the tail is possibly empty, if it's a 1 element list))

filter (<x) xs - A list of all elements in xs that are < x
filter (>= x) xs - Guessing you can infer this from above

++: concatenate/append lists, e.g [x,y,z]++[1,2,3] = [x,y,z,1,2,3]

qsort (... <x) ++ [x] ++ qsort ( .. >= x )
The (sorted) elements below x, then x, then the (sorted) elements above x

If it's a one element list, then this will be
(qsort []) ++ [x] ++ (qsort [])
and qsort [] = [], so [x]
>>
>>54804732
Both looks FUCKING ugly.
I want to write and read nice code, that can be understood. I do not want to fucking code golf when I am working on a project, or trying to track down a bug.

Fucking hipsters and webdevs, they should all burn in hell.
>>
>>54804732
Bet you love the ternary operator as well. Faggot.
>>
File: 91i328.jpg (142 KB, 740x740) Image search: [Google]
91i328.jpg
142 KB, 740x740
>>54815105
>non expression based languages
>>
>>54815105
What's wrong with it?
>>
>>54804732

I think it's cool that you can make differnt solutions in Haskell and Ruby that look so similar to each other:

# ruby version 1 (partition)
def qsort(list)
return list if list.size <= 1
x = list.pop
left, right = list.partition { |e| e < x}
qsort(left) + [x] + qsort(right)
end

# Haskell version 1 (partition)
import Data.List

qsort [] = []
qsort (x:xs) = left ++ [x] ++ right
where (left, right) = partition (<x) xs

# ruby version 2 (two criteria)
def qsort(list)
return list if list.size <= 1
x = list.pop
qsort(list.select { |xs| xs < x } ) + [x] + qsort(list.select { |xs| xs >= x } )
end

#haskell version 2 (two criteria)
qsort [] =[]
qsort (x:xs) = qsort(filter (< x) xs) ++ [x] ++ qsort(filter (>= x) xs)



>>54808832

>Scala took many of Ruby's concepts

See a comparison here:
>https://medium.com/@fapoli/is-scala-the-new-ruby-8d53975e7297#.olgwy4mcy

or here:
>http://de.slideshare.net/El_Picador/scala-vs-ruby


>>54809495

>Swift took many of Ruby's concepts

See here:
>http://www.littlelines.com/blog/2014/06/11/why-rubyist-will-love-swift/


>>54815065

>I want to write and read nice code, that can be understood.

So which line of ruby was unclear to you?

>Fucking hipsters and webdevs

Yeah, stereotypes are cool when you are 14.
>>
>>54804732
def qsort(xs)
return xs if xs.size <= 1
x = xs.pop()
qsort(xs.find_all {|e| e < x}) + [x] + qsort(xs.find_all {|e| e >= x})
end
>>
>>54804732
i don't know ruby but i can read what ruby's doing. i can't read what haskell's doing at all
>>
>>54816067
Lmao at that swift comparison
Ruby did not invent 4/5 of the things, I can't comment on that named string Concat though
>>
>>54817510

>Ruby did not invent 4/5 of the things

Right!

The guy who invented Ruby worked within a research group for programming languages at a University. Then he took his time to take the best parts of his favorite langauges (especially Perl, Smalltalk, Eiffel, Ada, and Lisp) and added cool features from other languages until he foudn a mixture that felt perfect for him in terms of productivity and fun:
>"Ruby is simple in appearance, but is very complex inside, just like our human body."


-pure object orientation and dynamic typing like small talk
-blocks (closures) and no difference between expressions/statements/code/data like Lisp (so "everything is an expression" and "everything is an object")
-powerfull RegEx's, "background magic" and "more than one way to do it" philosophy like perl
-lazy evaluation and powerfull Array handling like functional languages (map, inject, select, reverse, ...)
-private methods/attributes and single inheritance from Java
-modules like Objective-C
-mixins, easy and intuitive syntax, operator overloading and "usage of C as language underneath" and "easy extensions in C" like python
>>
>>54809318
>some site mentioned in the pic triggers him
this is how to spot reddit tourists. normal people don't care, because they never visited it.
Thread replies: 120
Thread images: 5

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.