43 points · by YAFZ · 13y ago · 73 comments
To me, this type of development is much more natural and sane than the "one big clusterfuck" type of projects I've seen in Java et.al.
Edit: Also, dynamic languages let you make the tradeof between safety and speed. Sometimes you go slow and steady (simple code, tests..) and sometimes you just want to test something out.
implicit class RichSeq[A, C[A] <: Seq[A]](underlying: C[A]{
def cycle: Iterator[A] = {
lazy val circular: Stream[A] = underlying.toStream #::: circular
circular.iterator
}
}All it does is convert a Seq (sequence) to a lazy stream that will infinitely cycle through the values.
Seq(1, 2, 3).cycle.take(8).toList
res3: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2)
It will work for any seq-like structure (including List. Vector, Queue, etc.)If you try to use it with anything else, like a Map, it won't compile.
Also, note from my example that it's generic and the resulting List is of the correct type.
Just like novice JavaScript developers struggle with "Why 'this' changes here?", it requires some experience with the language.
I don't think so. It is full of syntactic noise, for what is really just notation for building a cyclic structure.
* type annotations stated explicitly, not inferred
* type variables given multiple times
* too much non-verb, non-noun syntax e.g. `#:::`
* and the actual construction of the cycle is stated imperatively with great ceremony (despite it being an applicative)
It is the opposite of dense.
cycle seq = circular where circular = seq ++ circular
(or if you want to really golf it) cycle = fix . (++)
Ok, to be fair the Scala code is a bit more generic as it works for any abstract sequence, but still, Scala's syntax is awfully heavy compared to Haskell.That's one reason I still prefer Haskell even though Scala is easier to sneak into enterprise projects thanks to JVM. Not to mention that Haskell gives stronger static guarantees and has a more sophisticated type inference system.
cycleM = fix . mplus
gives a nice generic version that works on Lists & Sequences but falls afoul of the monomorphism restriction, so it needs a type: cycleM :: (MonadPlus m) => m a -> m a
cycleM = fix . mplusScala, Haskell treat syntax as a chore.
> take 8 $ cycle [1,2,3]
cycleM x = x `mplus` (cycleM x)
take 8 $ cycleM [1,2,3]
might be more generic?Regarding Scala's complexity - some more advanced features are presented in those slides. Imagine watching a talk about Python where they talk about decorators and meta-classes.
[1] https://www.youtube.com/watch?feature=player_embedded&v=grvv...
Are the Ruby complaints in the slides real - do people do those sort of things often and expect other languages to behave like that too? Is that because of the standard library or innate to the language?
Here's a Ruby snippet. I don't want to pick on this particular project [1], rather, I've found that this is typical of Ruby code:
state_machine :state, initial: :active do
after_transition any => :blocked do |user, transition|
# Remove user from all projects and
user.users_projects.find_each do |membership|
return false unless membership.destroy
end
end
My conclusion? Ruby's syntax is awful. There are colons, absolute value bars, implications, and who-knows-what-else flying everywhere! It gets worse, elsewhere in the same file there are messy lines like this one with single-arrow implications, question marks and ampersands [2] [3]: scope :not_in_project, ->(project) \
{ project.users.present? ? \
where("id not in (:ids)", ids: project.users.map(&:id) ) : scoped }
Simple, intuitive syntax? From where I sit, the syntax of Ruby is worse than C++, and approaches Perl levels of awfulness. In most languages, I can at least sort-of grok what's going on from the context when I see unfamiliar operators, but not so in Ruby.This is a copy-paste of a comment I made [4] weeks ago on another article.
[1] https://github.com/gitlabhq/gitlabhq/blob/4caaea824cf51670d1...
[2] https://github.com/gitlabhq/gitlabhq/blob/4caaea824cf51670d1...
[3] I split the line and inserted backslashes because HN gives me a horizontal scrollbar when it's a single long line; apologies if this transformation isn't legal Ruby, or changes the meaning of the code.
I think you're making the mistake that because Ruby isn't a C-style language, the equivalent of a Latin script language, it's not understandable. It's just disadvantaged that you can't immediately make some assumptions about what the operators do based on previous experience.
First off, parenthesis in ruby are optional. So calling a method can be done without parenthesis. The following two lines are the same:
print()
print
As are these: print(1, 2, 3)
print 1, 2, 3
Here's some bits that might explain better what stuff does: :state
If you prefix a word with a colon that creates an object of type Symbol, with value "state", which is a bit like the string "state". The difference is that every mention of :state is a reference to the same object, while if you create a string "state", and a bit further do it again, those are two different objects. We use symbols because it runs faster for conditionals etc: to compare two symbols the interpreter only needs to check the reference, while to compare two strings, every character needs to be checked. initial: :active
This creates a Hash with one key-value, with key :initial, and value :active, both symbols. This is equivalent to typing :initial => :active which you encounter a bit further up. do |var1, var2| .... end
This creates a block, which is like a closure. The variable names between pipes are the arguments this block takes. Any code inside the block is only run when the block is called. In Ruby, every function can have a block passed by adding do ... end to it. Inside this function, the block can be called using the keyword yield, e.g. yield(1,2) to call the block with arguments 1 and 2. The find_each method will loop through every value of the Array it is called on, and run the block once for every value. project.users.present?
The question mark is a valid character for a method name, there's nothing special happening here, present? is just the name of a method. This could easily have been called is_present, or just present. a ? b : c
The ternary operator, the same in any language. It translates to "if a, return b, else return c". ->(arg1, arg2) { ... }
This creates a lambda, with arguments arg1 and arg2. A lambda is almost the same as a block, so this is almost the same as typing do |arg1, arg2| ... end* Use setuptools, easy_install, pip while we have fun with our rake and gems.
* lambda : Please have fun, with this vomit of a feature.
* __init__.py explain that to a python beginner.
* Have fun with, { "quote": "shit" } while we use our :symbols
* What scope ?
* Have you migrated code to class Foo(object): yet ?
* How is py3 coming along ? Having fun with the worlds cleanest and bossy language community ?
* Write all your list comprehensions line noise, while we add a elegant method.
I just realized, whitespace is also, precisely the entire content of a pythonista's head.
http://logic.cse.unt.edu/tarau/teaching/SCALA_DOCS/scala-for...
And take the "in Scala" part of the course title with a grain of salt. You're only taught enough to complete the exercises.
I'm happy he found a language he enjoys working with, but I doubt this will change anyone's mind...
For example:
val i = 1;
i = "some string" // throws a compiler error, because it knows i is an integer
It's great when you haven't declared the types, you change for example an int to a double or a class to some other class (eg swapping a data structure) and it just flows through the code base without problems like a dynamically typed language.
It can also be pretty useful to look up types when they get complex in the middle of some function, like a Map[List[(String,SomeObject)]] (a map of lists of (String,SomeObject) tuples). Allowing the types to get complex lets me focus on the problem while giving a crutch to quickly remember where it's at half way through (and while finding other methods/source of data) and keep moving towards the solution.
I noticed a lack of a link to a 1:1 comparison of application code with realistic examples. I think such comparisons and related discussion are often the best way to convince someone to use a language.
And some slides were just blatantly wrong, like Mixin Abuse: apparently that is just a long list of module includes? I have never seen so many module includes in a single class in application code. But, assuming you did have that, you should show what it would look like side-by-side in Scala. In Ruby, there are a lot of options when it comes to including other code or defining code in a class, instance, etc. Those options can lead to much less and more legible code if used correctly.
I remember when people said Java was slow; they made it seem like it was a fad, and no reputable company would use it. And... we see where that went.
(a) Python isn't fast enough, and Pypy doesn't make it fast enough, or isn't a viable option in the target environment. So you write in C, OpenCL or assembly language.
(b) A vital library or other dependency can't talk to Python and you can't quickly find or write a reimplementation or bridge.
(c) Python isn't supported in the target environment, for example, the web browser [1] or the iPhone.
[1] Yes, yes, I know, things like Skulpt and Pyjamas exist. But I think those dependencies are too heavy for a lot of projects.
Also, Tcl/Tk called and wants its use case back.
Highly dynamic languages, such as Ruby, certainly have their advantages too. But programs in languages which enable, if not encourage, developers to add new methods to the integer '5' can quickly become very difficult to reason about.
Static, strongly-typed languages also provide other benefits such as much better error checking and compile-time optimization.
In Python, a private field starts with an underscore, and someone who knows the culture of Python knows that the initial underscore encodes the notion "This field is not considered to be part of this library's public API, and it may go away or change behavior at any time, not necessarily a major version bump of this library, and if you look at it or change it, you'll be responsible for maintaining your code when we break it. Also it's probably totally undocumented, so you'd better read the library code to make sure it does what you think it does before you use it."
A good developer will weigh the work that can be accomplished today by using the private field against the future consequences of the underscore's admonition, and come to a wise decision -- if (s)he decides to use the field, the Python language will defer to the programmer's human judgment and allow it.
A mediocre developer won't understand the underscore's implications, and will blithely use private and public fields in exactly the same way, because Python won't stop them.
In Java, the language itself will prevent this from occurring -- a private field, in Java, is really private. So the compiler [1] prevents mediocre developers from producing brittle code by referencing private fields everywhere.
If you have a lot of mediocre developers in your organization -- which IMHO tends to happen more in larger organizations -- then you want a stricter compiler to stop them from writing unmaintainable code.
I personally prefer the Python way, simply because I've been bitten by this Java "feature" on multiple occasions -- a third-party library marks some field as private, but I really want to use it, to the extent of being willing to deal with instability by updating my code or freezing the dependency, if necessary. But I can't, without making my own fork of the library.
[1] The runtime also prevents private field access. So even if you bypass the compiler's checks by patching it or hex editing the compiled bytecode, you'll still get runtime errors from trying to read private fields outside the class where they're defined.
Googling about SBT came back with a lot of complaints about the state of scala tooling back then.
[1] https://github.com/yareally/android-scala-intellij-no-sbt-pl...
Seems like the tooling is still one of the more painful aspects of Scala. Kudos to Intellij, -1 to Scala.
Excessive configuration to get a new project up in a running is a major demotivator sometimes when you have a new idea and want to just start coding right away.
If C has Valgrind then dynamic typed languages should get something for the types.
http://blog.jetbrains.com/pycharm/2013/02/dynamic-runtime-ty...