JeuWeb - Crée ton jeu par navigateur
Ruby ou Python ? - Version imprimable

+- JeuWeb - Crée ton jeu par navigateur (https://jeuweb.org)
+-- Forum : Discussions, Aide, Ressources... (https://jeuweb.org/forumdisplay.php?fid=38)
+--- Forum : Programmation, infrastructure (https://jeuweb.org/forumdisplay.php?fid=51)
+--- Sujet : Ruby ou Python ? (/showthread.php?tid=6088)

Pages : 1 2 3 4


RE: Ruby ou Python ? - srm - 04-05-2012

Merci Smile


RE: Ruby ou Python ? - niahoo - 04-05-2012

(03-05-2012, 03:21 PM)oxman a écrit : Moi depuis que je connais Scala qui allie en gros le meilleur du fonctionnel et de l'objet, je ne peux pas me résoudre à me suffire d'un langage uniquement fonctionnel.

Tiens, lis-ça, c'est très partisan mais tu comprendras peut-être pourquoi il te semble ne pas pouvoir te suffire d'un langage uniquement fonctionnel en ayant choisi scala.

http://blog.enfranchisedmind.com/posts/scala-not-functional/


RE: Ruby ou Python ? - srm - 04-05-2012

C'est intéressant comme article, du moins surtout les commentaires Wink
Je vais mettre ici le plus intéressant (car l'auteur de l'article ne connait pas assez bien le Scala)
Citation :Things aren't quite as verbose as you make them. I'll paste in some Scala interpretter sessions. OCaml's:

let f x = x + 1;;

looks like this in Scala:

def f(x: Int) = x + 1

For example:

scala> def f(x: Int) = x + 1
f: (Int)Int

scala> f(2)
res0: Int = 3

You can do currying in a few ways in Scala. For OCaml's:

let x a b = a + b;;
let y = x 1;;
y 2;; (* 3 *)

You can either define a function with two parameter lists:

scala> def x(a: Int)(b: Int) = a + b
x: (Int)(Int)Int

scala> x(1)(2)
res1: Int = 3

To leave out a parameter in Scala, you need to put an underscore to signal your intent to actually leave one out. This is because Scala to have good cross compatibility with Java libraries needed to kind of adopt the overloading rules. So you could have an x method with one parameter and an x with two. So it doesn't work to just do what they do in languages where currying is the default. So it looks like this:

scala> val y = x(1) _
y: (Int) => Int =

That underscore tells the compiler I did indeed mean to leave off the 2nd param, so I get currying:

scala> y(2)
res2: Int = 3

Another way to do currying is to just put both parameters in one parameter list, like this:

scala> def x(a: Int, b: Int) = a + b
x: (Int,Int)Int

Then put an underscore just for that parameter that you want to leave out. You also have to specify the type in that case too, again, because of overloading (the overloading was a compromise to be compatible):

scala> val y = x(1, _: Int)
y: (Int) => Int =

scala> y(2)
res3: Int = 3

The set of case classes you showed is correct, except you don't need to seal the subtypes. (And you don't need those semicolons either!) It could just be:

sealed abstract class Robert
case class Foo(value:Int) extends Robert
case class Bar(value:String) extends Robert
case class Baz extends Robert

That is more verbose than OCaml's:

type robert = Foo of int | Bar of string | Baz;;

But although I don't know OCaml, I suspect there's more you can do with case classes. You can put behavior in them. You can subclass them too. What they were talking about removing was letting you subclass a case class with *another case class*, but you'd still be able to subclass them. So you get the pattern matching plus all the OO stuff.

En effet le fonctionnel de Scala est plus verbeux, mais ne semble pas en tout cas particulièrement moins puissant, c'était sans doute abusé de dire "le meilleur du fonctionnel", mais en tout cas pas loin.
Un autre commentaire que j'ai bien aimé :
Citation :Scala was built to scale up, not scale down.

Car en effet Scala est conçu comme un micro kernel de règles et tout le reste est fait en librairie, n'importe qui, même toi peut facilement étendre Scala pour lui ajouter un truc qui te manque, une syntaxe qui te manque, ou presque ce que tu veux.


RE: Ruby ou Python ? - niahoo - 04-05-2012

Mouais c'est vrai que tu m'avais passé un tuto et que j'avais vu des méthodes de currying beaucoup plus simple que celle présentée par l'auteur de l'article.

Moi ça me gêne ces underscores dans tous les sens ou ces doubles parenthèses, ça rend le code brouillon. (alors que c'est censé être super méga élégant, mais … non).

Bon et alors, tu nous a codé un truc avec scala, qu'on voie un peu !?!


RE: Ruby ou Python ? - srm - 04-05-2012

Et bien je suis d'accord oui et non avec toi concernant les parenthèses et l'underscore.
Je trouve ça plus lisible : val cinq_premiers = take(5) _
Que ça : cinq_premiers = take 5
Ou tu ne comprends pas que cinq_premiers prend un argument, tu dois regarder la fonction take pour le savoir, au moins en Scala c'est plus lisible.
Donc dans ce cas présentement, je préfère le _.

Pour les parenthèses : def x(a: Int)(b: Int) = a + b
Ca permet d'appeler la fonction comme ça :
x(3) {
un bloc de code que je veux
}

Donc je trouve que c'est utile les parenthèses, c'est quoi l'écriture équivalente pour ça en Haskell/Erlang ou autre ?


RE: Ruby ou Python ? - niahoo - 04-05-2012

L'underscore peut servir à se rappeller qu'il faut un argument c'est pas faux. (bon dans ce cas présent la fonction take tu la connais bien).

ben en erlang il n'y a pas de currying, la syntaxe est plutot lourde d'ailleurs.

En haskell en gros tu remplaces tes accolades par des parenthèses pour y mettre un bloc de code)
ça peut être moche sans l'indentation

[edit : code qui marche pas, plus le temps de corriger aujourd'hui]