What is your favourite function()?
endan

endan @jjjjcccjjf

About: Reese’s peanut butter cup-fueled coding monster who dwells in the web. Also devours books, video games, anime, and manga. I write about everything!

Location:
Manila
Joined:
Nov 2, 2017

What is your favourite function()?

Publish Date: Jul 3 '18
38 36

I think mine would be json_encode(). I think it's so cool that a single function can make your data understood by other machines. (I know other machines can interpret different kinds of data, but c'mon...)

How about you? What's your favourite function?

Comments 36 total

  • Meghan (she/her)
    Meghan (she/her)Jul 3, 2018
    new Date()
    
  • Avalander
    AvalanderJul 3, 2018
    explain_fork_or_die();
    

    Reference

  • TJ Fogarty
    TJ FogartyJul 3, 2018

    Based on usage it's probably console.log() or var_dump().

    At the moment I'm enjoying some JavaScript array methods - filter, map, reduce.

    • endan
      endanJul 3, 2018

      var_dump (); die ()!!!

      • Tiago Marques
        Tiago MarquesJul 3, 2018

        I usually do like:
        die(var_dump());

        • endan
          endanJul 4, 2018

          I stopped doing this simply because I can't do it if I dump multiple variables at once.

          var_dump($res);
          var_dump($res2);
          die();
          
  • 31547
    31547Jul 3, 2018

    typeof(arg);

    surprisingly useful

  • Adrian B.G.
    Adrian B.G.Jul 3, 2018

    random() functions, there is so much more behind them then we usually think. I can talk hours about entropy and sources, techniques, pseudo algorithms and how can it improve almost any software product.

    • endan
      endanJul 4, 2018

      Improve in what way?

      • IsaacLeimgruber
        IsaacLeimgruberJul 4, 2018

        In the way of randomized algorithms

      • Adrian B.G.
        Adrian B.G.Jul 4, 2018

        As in, some product features can be improved using random (shuffle or generate things), most basic example is chose new 5 news headlines to see from the top 100 articles to an user, each x seconds/page views.

  • John Alcher
    John AlcherJul 3, 2018
    $("");
    

    Because it took me almost half a year to realize that it's a function, not a language construct.

  • Jishnu Vasanth
    Jishnu VasanthJul 3, 2018

    Arrow function 🤓. this has never been easier.

  • essic
    essicJul 3, 2018

    mine is fmap ...
    fmap :: (a -> b) -> F a -> F b
    with F being a Functor :-)

    I find this one, truly comforting for some reason.

  • Bertil Muth
    Bertil MuthJul 3, 2018

    I like map.put and map.get. Maps/Dictionaries are incredibly useful data structures that we tend to take for granted nowadays.

  • juankOrtiz
    juankOrtizJul 3, 2018

    Don't know if it's only a function, but ajax in Javascript is something else: the whole idea of a function connecting asynchronously to a server and retrieving data without the need of recharge the entire page is amazing.

  • ItsASine (Kayla)
    ItsASine (Kayla)Jul 4, 2018
    expect(true).toBe(true);
    
    1. It's a super useful dumb placeholder when just building out describes, its, beforeAlls, etc in Jasmine tests without actually caring about the legit expectations yet.
    2. It actually failed for me once, which was... enlightening
  • Jundi Alwan
    Jundi AlwanJul 4, 2018

    map, filter, reduce, forEach, Array.prototype.some, Array.prototype.every

    Working with array in Javascript never been easier.
    Love it all!

  • Max Maxymenko
    Max MaxymenkoJul 4, 2018

    Love throwing exceptions around ;)

    throw new Exception("...");
    throw Error("...");
    raise Exception.Create('...');
    
  • Andžs
    AndžsJul 4, 2018

    In PHP I always include file with my custom helper functions. These two wrap output with <pre/> tags, which allows nice&quick debugging via browser from any class or template code.

    
    function pre($obj = null, $escape = false){
        echo "<pre>";
        if($escape){
            $obj = htmlspecialchars ( print_r($obj, true) );
        }
        print_r($obj);
        echo "</pre>";
    }
    
    function dump($obj = null){
        echo "<pre>";
        var_dump($obj);
        echo "</pre>";
    }
    
    • endan
      endanJul 4, 2018

      var_masterpiece is also an excellent browser extension too for debugging!

    • Dan Fellini
      Dan FelliniJul 4, 2018

      HA! I have a pre() function too! I use it many, many times a day. Mine has the $obj, but then a title, so if I have multiple pre()s going, I know which is which. Like, pre( $obj, 'This is the user obj');
      Love it.

  • Josh Cheek
    Josh CheekJul 4, 2018

    In Ruby, backticks are a function call. And what's especially cool is that heredocuments with backticks call the same function, but with multi line arguments. So you can define them to do other, more interesting things, like call out to other languages! In practice, I mostly use it when experimenting with some slightly tedious external resource, I basically learned PostgreSQL by running them through these kinds of functions.

    require 'open3'
    
    def `(str)
      Open3.capture3('php', stdin_data: str).first
    end
    
    <<~`PHP` # => "hello cruel world\n"
    hello \
    <?php echo "cruel" ?> \
    world
    PHP
    
  • IsaacLeimgruber
    IsaacLeimgruberJul 4, 2018

    list.zip (other).map ((a,b) =>...)

  • Quentin Caillaud
    Quentin CaillaudJul 4, 2018

    I don't know why but I just love sprintf() in php ... It's very useful and much more elegant than just endlessly concatenate strings !

    And less elegant but really useful once again, die(var_dump()) has a special place in my heart ...

  • everydayGenius
    everydayGeniusJul 4, 2018

    When something went unexpectedly wrong..

    panic(error)
    
  • Abhishek Nair
    Abhishek NairJul 5, 2018

    reduce is my favorite.

  • Frederik 👨‍💻➡️🌐 Creemers
    Frederik 👨‍💻➡️🌐 CreemersJul 5, 2018

    I don't really have a favorite function, because it's all about using the right one to solve a problem. ut I like functions that make things simpler, so I think I'd pick fetch(). Because before, making HTTP requests from JavaScript looked like this

    var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
         document.getElementById("demo").innerHTML = this.responseText;
        }
      };
      xhttp.open("GET", "ajax_info.txt", true);
      xhttp.send();
    
  • Nigel
    NigelJul 5, 2018

    curry function is probably my prefered one.
    For those who don't know its utility, here's a basic example:

    const divide = curry((a, b) => a / b)
    const divide4By = divide(4)
    
    console.log(divide4by(2))
    // 2
    

    I even made my own optimized version 😄
    npmjs.com/package/super-curry.

Add comment