5 ms·
My heart grew a little warm with the last paragraph of John Carmack's comment: "The major evolution that is still going on for me is towards a more functional
by jlongster 14y ago
My heart grew a little warm with the last paragraph of John Carmack's comment:
"The major evolution that is still going on for me is towards a more functional programming style, which involves unlearning a lot of old habits, and backing away from some OOP directions."
- zaphoyd 14y agoHas anyone here come across any particularly good books or other resources on functional style programming in C++? Especially ones that have been updated for C++11s new functional features.
- gits1225 14y agoBartosz Milewski's blog: http://bartoszmilewski.com/ http://bartoszmilewski.com/
- quux 14y agoJohn Carmack wrote a nice article about experiences writing functional code in C++ here: http://www.altdevblogaday.com/2012/04/26/functional-programming-in-c/ http://www.altdevblogaday.com/2012/04/26/functional-programm...
- mark-r 14y agoThat very link was at the bottom of his original response.
- quux 14y agoAh, didn't notice it. Thanks. If I missed it, I can't be the only one.
- worldsayshi 14y ago"a function can still be pure even if it calls impure functions, as long as the side effects don’t escape the outer function" This is a very good point that probably could be systematically exploited. Does anyone know examples of this?
- greiskul 14y agoI think caching/memoization is one of the most typical cases of this.
- ufo 14y agoIn Haskell there is the ST monad can be used to write stateful implementations for pure functions. The type system guarantees that side effects can't escape their scope. http://www.haskell.org/haskellwiki/Monad/ST http://www.haskell.org/haskellwiki/Monad/ST
- worldsayshi 14y agoAlthough this is great information I primarily had 'less pure' languages' in mind when I asked. Come to think of it, restricting mutable access to only the relevant objects as per function call using const seems to allow just this! A function having only const input and output "should be" free of side effects (if no global mutables); while still being allowed to run all manner of unpure processes local to its own calling context.
- mattdw 14y agoClojure has "transient" (mutable) forms of its immutable datastructures, which can be bashed in place, but they're not allowed beyond function boundaries in either direction (I think). So you convert (copy?) to transient, mutate it in-place, then convert back to "persistent" before returning it.
- crntaylor 14y agoHere's a pretty trivial example in Haskell - computing the factorial function using a mutable variable. import Control.Monad.ST import Data.STRef fact :: Int -> Int fact n = runST (fact' n) fact' :: Int -> ST s Int fact' n = do a <- newSTRef 1 mapM_ (\x -> modifySTRef a (*x)) [1..n] readSTRef a Here the function `fact'` uses mutable variables (encoded in the use of `ST` in its type -- `ST` stands for State Thread) but the function `fact` is pure -- the call to `runST` ensures that none of the side effects leak out of `fact'`. As with most Haskell code, the types are optional - I included them for clarity.
- agumonkey 14y agoTim Sweeney of Epic also praised functional programming few years ago http://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/sweeny.pdf http://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-... I don't know what's his stance on the subject since though.
- AlexDanger 14y agoHe uses C# to provide examples of dynamic failure in compiler checked code. Since then, some of his 'what we want to write' pseudo-code examples are now reality in C#.
- Cowen 14y agoHe's mentioned before that he's toyed with the idea of switching to Haskell. That wasn't for functional programming though. It was instead for the built-in static code analysis the Haskell type system and compiler provides.
- graue 14y agoDo you have a link to the source for that? I'm curious to read. If your description is accurate, Rust[1] is exactly what he wants. I've been learning Rust recently (with a little help from [2]) and it does a ton of static code analysis, is safe by default (no dangling or null pointers, no shared mutable state), uses Hindley-Milner type interface just like Haskell, and generally is what you would expect if Haskell and C++ had a baby. [1] http://www.rust-lang.org/ http://www.rust-lang.org/ [2] http://www.rustforrubyists.com/ http://www.rustforrubyists.com/
- gnuvince 14y agoRust is pretty much exactly what a lot of people want and need; a high level language with a strong focus on safety and concurrency, while still allowing you to control the lower level details such as memory layout for good performance.
- ansible 14y agoI'll say this for them, they know how to write an introductory home page. All too often, I've heard about some language, and then spent minutes trying to figure out what it is all about. Rust is definitely worth a longer look by me.
- gtani 14y agoHe sort of alluded to it, but there's the usual perceived tooling/ retraining/hiring / performance ceiling issues for shops with > N devs, N somewhere between 5 and 25 (I don't agree with his arguments, just repeating them) http://gamasutra.com/view/news/169296/Indepth_Functional_programming_in_C.php http://gamasutra.com/view/news/169296/Indepth_Functional_pro... http://www.reddit.com/r/haskell/comments/jap3x/im_very_tempted_to_want_to_move_to_a_functional/ http://www.reddit.com/r/haskell/comments/jap3x/im_very_tempt... http://blogs.uw.edu/ajko/2012/08/22/john-carmack-discusses-the-art-and-science-of-software-engineering/ http://blogs.uw.edu/ajko/2012/08/22/john-carmack-discusses-t...