Hacker Newsnew | past | comments | ask | show | jobs | submit | tikue_'s commentslogin

Backwards-compatibly changing existing features feels qualitatively the same to me as adding new features. In either case you can ignore them until you need to work on a codebase that uses them.


At least in Rust, capture granularity is per-variable, so I think closures don't contain a stack frame pointer? Though, I'm not sure why this would matter to a user evaluating Rust's closures vs. D's nested fns?


It’s a pointer to the environment, and a pointer to the function. So yes, not a stack frame pointer.

The environment is a struct containing what is captured; so for example, it would contain an &T if your closure captures a T by reference. You don’t need to walk a set of stack pointers to find it, just follow the reference directly there.


That's distinctly different from D's, which has a pointer to the enclosing stack frame.

If it is the enclosing function, it's just a single pointer to it. The "walking" comes from if you're accessing the enclosing function's enclosing function's frame.


Makes perfect sense, thanks. I’m now wondering about the trade offs of these two approaches...


Saying library authors are content with their absence is probably a bit too strong. There's not really another option, and I doubt many folks would choose not to author libraries for this reason alone. I personally have found their absence annoying when writing Rust libraries.

I'd probably be in favor of a hard cap on # of function arguments. :)


But there is another option: a configuration struct, potentially combined with an implementation of Default. Indeed, part of the reason this conversation has gone on so long is that there's one camp who just wants to emulate keyword arguments via additional sugar for structs and Default.

(There's also another another option, which is to have a different function for each combination of parameters. This obviously doesn't scale in the large, but it's perfectly acceptable for functions that take only a single optional parameter, which IME is a plurality of APIs that want optional parameters).


Functional typically implies higher-order functions, of which there are none in that Python code.


There is no well defined definition of functional programming. Since almost every mainstream imperative language supports higher-order functions, I would suggest that criteria is inadequate. A better definition is programming without side-effects.


Output is a side effect.


Not in Haskell and other pure languages. Pure functional programming is used to construct larger programs from smaller ones, which only when finally executed by the runtime produce output.


Are there any programs that produce output before they are executed?


It is not easy describing pure functional programming in two sentences. I encourage you to read up on it yourself. Again, IO is not a side-effect in pure languages such as Haskell. And again, the absence of side-effects best defines functional programming, not any particular feature, which imperative languages can and do steal.


What's it like working with Rust full-time? Is it similar to using any other language for a job? Do you find yourself enjoying coding more? Do you get unnecessarily caught up in micro-optimizations for things you wouldn't blink at in Go/Java?


Every language has different idioms. In many cases, what works in C++ doesn't work in Rust. Generally, anything the book says is bad you should take as qualified to mean bad in the context of Rust programming, not programming at large.


Sorry, I didn't make it clear enough. I was primarily referring to code formatting. Spaces instead of tabs, for example, or not writing opening curly braces on separate lines. These things are a matter of taste and should be friendly recommended at best. Guidelines for idioms are important and should occur as early as possible in the book, so that's perfectly fine.


I don't think telling you how to format your code for a particular language is a bad thing. I am quite happy using { on a new line in C# but on the same line in JS, using underscore_separated_method_names in C and camelCasedMethodNames in Java. To me, once you've been round the block with more than one or two languages you can get used to whatever style, but using the style for the language that you're writing in is something you should try to do for other people who read your code.

Reading Java code that uses undercore_separated_method_names or C# code that uses camedCased immediately makes me think the author doesn't know what s/he's doing.


They have them in the Czech Republic, too. I've been on one in Prague.


I wish `and_then` were named `flat_map` so badly.


We have `flat_map` for iterators. (`and_then` works for Options, which are basically nullables)

You can convert an Option to a zero-cost iterator and then use `flat_map` though.


Also seems like you could benefit from the entry() api (available on both BTreeMap and HashMap):

http://doc.rust-lang.org/std/collections/struct.BTreeMap.htm...

I think the example used in the docs is your exact use case.

This bit:

  let found = match map.get_mut(..) {
    ..
  }
  if !found {
    ..
  }
can be replaced with

  match map.entry(word) {
      Occupied(mut view) => { *view.get_mut() += 1; }
      Vacant(view) => { view.insert(1); }
  }


Good point! I’ve updated the article accordingly. I learned the Entry thing a few months ago, but Rust’s BTreeMap did not support the entry API at that time, so my code did not use it. Then I totally forgot about it when writing this blog...


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: