SlideShare una empresa de Scribd logo
1 de 58
Descargar para leer sin conexión
Functional
                    Programming


Friday, 18 May 12
Andrew Jones @andrew_jones
                    Thom Leggett @thomleggett



Friday, 18 May 12
BrisFunctional
                          @brisfunctional
                     brisfunctional.github.com




Friday, 18 May 12
Project Euler



Friday, 18 May 12
JSON parser



Friday, 18 May 12
Noughts and Crosses
                            AI


Friday, 18 May 12
http://www.flickr.com/photos/dippy_duck/7210706592/




Friday, 18 May 12
Alligator Eggs

Friday, 18 May 12
Friday, 18 May 12
http://www.flickr.com/photos/14591708@N00/6878586390/




Friday, 18 May 12
What is Functional
                     Programming?


Friday, 18 May 12
Functional




Friday, 18 May 12
Disfunctional   Functional




Friday, 18 May 12
Disfunctional        Functional


                      C#




Friday, 18 May 12
Disfunctional        Functional


                      C#




Friday, 18 May 12
Disfunctional        Functional


                      C#    Haskell




Friday, 18 May 12
Disfunctional                     Functional


                      C#                 Haskell
                           Javascript




Friday, 18 May 12
Disfunctional                         Functional


                      C#                     Haskell
                               Javascript
                           C




Friday, 18 May 12
Disfunctional                                Functional


                             C#                     Haskell
                    Pascal            Javascript
                                  C




Friday, 18 May 12
Disfunctional          Java                    Functional


                             C#                       Haskell
                    Pascal              Javascript
                                    C




Friday, 18 May 12
Disfunctional          Java                     Functional


                             C#                        Haskell
                    Pascal              Javascript
                                    C
                                                     Erlang



Friday, 18 May 12
Disfunctional          Java                     Functional


                             C#                        Haskell
                    Pascal              Javascript
                                                       Clojure
                                    C
                                                     Erlang



Friday, 18 May 12
Disfunctional          Java          SQL        Functional


                             C#                        Haskell
                    Pascal              Javascript
                                                       Clojure
                                    C
                                                     Erlang



Friday, 18 May 12
Python

      Disfunctional          Java          SQL        Functional


                             C#                        Haskell
                    Pascal              Javascript
                                                       Clojure
                                    C
                                                     Erlang



Friday, 18 May 12
Ruby
                                          Python

      Disfunctional          Java          SQL        Functional


                             C#                        Haskell
                    Pascal              Javascript
                                                       Clojure
                                    C
                                                     Erlang



Friday, 18 May 12
Ruby
                                                Python

      Disfunctional                Java          SQL        Functional


                                   C#                        Haskell
                    Pascal                    Javascript
                                                             Clojure
                                          C
                             PHP                           Erlang



Friday, 18 May 12
http://alblue.bandlem.com/2012/03/qcon-day-3.html




Friday, 18 May 12
Immutability



Friday, 18 May 12
Friday, 18 May 12
Friday, 18 May 12
1 public class Mutable {
             2     private List<Sheep> herd;
             3
             4     public List<Sheep> getHerd() {
             5         return herd;
             6     }
             7 }




Friday, 18 May 12
1 public class Immutable {
            2     private final List<Sheep> herd;
            3
            4     public List<Sheep> getHerd() {
            5         return Collections.unmodifiableList(herd);
            6     }
            7
            8 }




Friday, 18 May 12
Referential
                    Transparency


Friday, 18 May 12
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
First Class Functions



Friday, 18 May 12
http://www.flickr.com/photos/22748341@N00/2789592060/sizes/l/in/photostream/




Friday, 18 May 12
1 function shearHerd (herd) {
            2     for (var sheep in herd) {
            3         shear(sheep);
            4     }
            5 }
            6
            7 shearHerd(sheepHerd);




Friday, 18 May 12
http://www.flickr.com/photos/amypalko/3607158016/




Friday, 18 May 12
http://www.flickr.com/photos/emuphoto/206196733




Friday, 18 May 12
1       function map (herd, action) {
            2           for (var animal in herd) {
            3               action(animal);
            4           }
            5       }
            6
            7       map(sheepHerd, shear);
            8       map(cowHerd, milk);




Friday, 18 May 12
Examples



Friday, 18 May 12
LZW in C



Friday, 18 May 12
1   #include    <stdio.h>
      2   #include    <stdlib.h>                                                                67       ushort prev, back;
      3   #include    <string.h>                                                                68       byte c;
      4   #include    <stdint.h>                                                                69   } lzw_dec_t;
      5   #include    <unistd.h>                                                                70
      6   #include    <fcntl.h>                                                                 71   byte* lzw_encode(byte *in, int max_bits)
      7   #include    <sys/types.h>                                                             72   {
      8   #include    <sys/stat.h>                                                              73       int len = _len(in), bits = 9, next_shift = 512;
      9                                                                                         74       ushort code, c, nc, next_code = M_NEW;
     10   /* -------- aux stuff ---------- */                                                   75       lzw_enc_t *d = _new(lzw_enc_t, 512);
     11   void* mem_alloc(size_t item_size, size_t n_item)                                      76
     12   {                                                                                     77       if (max_bits > 16) max_bits = 16;
     13       size_t *x = calloc(1, sizeof(size_t)*2 + n_item * item_size);                     78       if (max_bits < 9 ) max_bits = 12;
     14       x[0] = item_size;                                                                 79
     15       x[1] = n_item;                                                                    80       byte *out = _new(ushort, 4);
     16       return x + 2;                                                                     81       int out_len = 0, o_bits = 0;
     17   }                                                                                     82       uint32_t tmp = 0;
     18                                                                                         83
     19   void* mem_extend(void *m, size_t new_n)                                               84       inline void write_bits(ushort x) {
     20   {                                                                                     85           tmp = (tmp << bits) | x;
     21       size_t *x = (size_t*)m - 2;                                                       86           o_bits += bits;
     22       x = realloc(x, sizeof(size_t) * 2 + *x * new_n);                                  87           if (_len(out) <= out_len) _extend(out);
     23       if (new_n > x[1])                                                                 88           while (o_bits >= 8) {
     24           memset((char*)(x + 2) + x[0] * x[1], 0, x[0] * (new_n - x[1]));               89               o_bits -= 8;
     25       x[1] = new_n;                                                                     90               out[out_len++] = tmp >> o_bits;
     26       return x + 2;                                                                     91               tmp &= (1 << o_bits) - 1;
     27   }                                                                                     92           }
     28                                                                                         93       }
     29   inline void _clear(void *m)                                                           94
     30   {                                                                                     95       //write_bits(M_CLR);
     31       size_t *x = (size_t*)m - 2;                                                       96       for (code = *(in++); --len; ) {
     32       memset(m, 0, x[0] * x[1]);                                                        97           c = *(in++);
     33   }                                                                                     98           if ((nc = d[code].next[c]))
     34                                                                                         99               code = nc;
     35   #define    _new(type, n)   mem_alloc(sizeof(type), n)                                100           else {
     36   #define    _del(m)     { free((size_t*)(m) - 2); m = 0; }                            101               write_bits(code);
     37   #define    _len(m)     *((size_t*)m - 1)                                             102               nc = d[code].next[c] = next_code++;
     38   #define    _setsize(m, n) m = mem_extend(m, n)                                       103               code = c;
     39   #define    _extend(m) m = mem_extend(m, _len(m) * 2)                                 104           }
     40                                                                                        105
     41                                                                                        106           /* next new code would be too long for current table */
     42   /* ----------- LZW stuff -------------- */                                           107           if (next_code == next_shift) {
     43   typedef uint8_t byte;                                                                108               /* either reset table back to 9 bits */
     44   typedef uint16_t ushort;                                                             109               if (++bits > max_bits) {
     45                                                                                        110                   /* table clear marker must occur before bit reset */
     46   #define M_CLR      256 /* clear table marker */                                      111                   write_bits(M_CLR);
     47   #define M_EOD      257 /* end-of-data marker */                                      112
     48   #define M_NEW      258 /* new code index */                                          113                      bits = 9;
     49                                                                                        114                      next_shift = 512;
     50   /* encode and decode dictionary structures.                                          115                      next_code = M_NEW;
     51      for encoding, entry at code index is a list of indices that follow current one,   116                      _clear(d);
     52      i.e. if code 97 is 'a', code 387 is 'ab', and code 1022 is 'abc',                 117                  } else /* or extend table */
     53      then dict[97].next['b'] = 387, dict[387].next['c'] = 1022, etc. */                118                      _setsize(d, next_shift *= 2);
     54   typedef struct {                                                                     119           }
     55       ushort next[256];                                                                120       }
     56   } lzw_enc_t;                                                                         121
     57                                                                                        122       write_bits(code);
     58   /* for decoding, dictionary contains index of whatever prefix index plus trailing    123       write_bits(M_EOD);
     59       byte. i.e. like previous example,                                                124       if (tmp) write_bits(tmp);
     60        dict[1022] = { c: 'c', prev: 387 },                                             125
     61        dict[387] = { c: 'b', prev: 97 },                                               126       _del(d);
     62        dict[97]   = { c: 'a', prev: 0 }                                                127
     63       the "back" element is used for temporarily chaining indices when resolving       128       _setsize(out, out_len);
     64       a code to bytes                                                                  129       return out;
     65    */                                                                                  130   }
     66   typedef struct {



Friday, 18 May 12
LZW in Clojure



Friday, 18 May 12
1 (defn make-dict []
         2   (let [vals (range 0 256)]
         3     (zipmap (map (comp #'list #'char) vals) vals)))
         4
         5 (defn compress [#^String text]
         6   (loop [t (seq text)
         7          r '()
         8          w '()
         9          dict (make-dict)
        10          s 256]
        11     (let [c (first t)]
        12       (if c
        13         (let [wc (cons c w)]
        14           (if (get dict wc)
        15             (recur (rest t) r wc dict s)
        16             (recur (rest t) (cons (get dict w) r) (list c)
        17                (assoc dict wc s) (inc s))))
        18         (reverse (if w (cons (get dict w) r) r))))))
        19
        20 (compress "TOBEORNOTTOBEORTOBEORNOT")




Friday, 18 May 12
LZW in Haskell



Friday, 18 May 12
1        import Data.List
           2        import Data.Char
           3        import Data.Maybe
           4
           5        doLZW _ [] = []
           6        doLZW as (x:xs) = lzw (map return as) [x] xs
           7           where lzw a w [] = [fromJust $ elemIndex w a]
           8                 lzw a w (x:xs) | w' `elem` a = lzw a w' xs
           9                                 | otherwise   = fromJust (elemIndex w a) :
          10                                                            lzw (a++[w']) [x] xs
          11                      where w' = w++[x]




Friday, 18 May 12
Friday, 18 May 12
Recommended Reading



Friday, 18 May 12
Friday, 18 May 12
Closing Thought
                     One thing that makes a programming language ‘more
                    powerful’ in my opinion is the provision of more ways
                      to factor programs. Or if you prefer, more axes of
                        composition. The more different ways you can
                       compose programs out of subprograms, the more
                                    powerful a language is.

                     Reg Braithwaite - Functional Programming Matters




Friday, 18 May 12
Thanks!
                    Andrew Jones @andrew_jones
                     Thom Leggett @thomleggett




Friday, 18 May 12

Más contenido relacionado

Último

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 

Último (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Functional Programming

  • 1. Functional Programming Friday, 18 May 12
  • 2. Andrew Jones @andrew_jones Thom Leggett @thomleggett Friday, 18 May 12
  • 3. BrisFunctional @brisfunctional brisfunctional.github.com Friday, 18 May 12
  • 6. Noughts and Crosses AI Friday, 18 May 12
  • 11. What is Functional Programming? Friday, 18 May 12
  • 13. Disfunctional Functional Friday, 18 May 12
  • 14. Disfunctional Functional C# Friday, 18 May 12
  • 15. Disfunctional Functional C# Friday, 18 May 12
  • 16. Disfunctional Functional C# Haskell Friday, 18 May 12
  • 17. Disfunctional Functional C# Haskell Javascript Friday, 18 May 12
  • 18. Disfunctional Functional C# Haskell Javascript C Friday, 18 May 12
  • 19. Disfunctional Functional C# Haskell Pascal Javascript C Friday, 18 May 12
  • 20. Disfunctional Java Functional C# Haskell Pascal Javascript C Friday, 18 May 12
  • 21. Disfunctional Java Functional C# Haskell Pascal Javascript C Erlang Friday, 18 May 12
  • 22. Disfunctional Java Functional C# Haskell Pascal Javascript Clojure C Erlang Friday, 18 May 12
  • 23. Disfunctional Java SQL Functional C# Haskell Pascal Javascript Clojure C Erlang Friday, 18 May 12
  • 24. Python Disfunctional Java SQL Functional C# Haskell Pascal Javascript Clojure C Erlang Friday, 18 May 12
  • 25. Ruby Python Disfunctional Java SQL Functional C# Haskell Pascal Javascript Clojure C Erlang Friday, 18 May 12
  • 26. Ruby Python Disfunctional Java SQL Functional C# Haskell Pascal Javascript Clojure C PHP Erlang Friday, 18 May 12
  • 31. 1 public class Mutable { 2 private List<Sheep> herd; 3 4 public List<Sheep> getHerd() { 5 return herd; 6 } 7 } Friday, 18 May 12
  • 32. 1 public class Immutable { 2 private final List<Sheep> herd; 3 4 public List<Sheep> getHerd() { 5 return Collections.unmodifiableList(herd); 6 } 7 8 } Friday, 18 May 12
  • 33. Referential Transparency Friday, 18 May 12
  • 34. http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 35. http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 36. http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 37. http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/ http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 38. http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/ http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 39. http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/ http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 40. http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/ http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 43. 1 function shearHerd (herd) { 2 for (var sheep in herd) { 3 shear(sheep); 4 } 5 } 6 7 shearHerd(sheepHerd); Friday, 18 May 12
  • 46. 1 function map (herd, action) { 2 for (var animal in herd) { 3 action(animal); 4 } 5 } 6 7 map(sheepHerd, shear); 8 map(cowHerd, milk); Friday, 18 May 12
  • 48. LZW in C Friday, 18 May 12
  • 49. 1 #include <stdio.h> 2 #include <stdlib.h> 67 ushort prev, back; 3 #include <string.h> 68 byte c; 4 #include <stdint.h> 69 } lzw_dec_t; 5 #include <unistd.h> 70 6 #include <fcntl.h> 71 byte* lzw_encode(byte *in, int max_bits) 7 #include <sys/types.h> 72 { 8 #include <sys/stat.h> 73 int len = _len(in), bits = 9, next_shift = 512; 9 74 ushort code, c, nc, next_code = M_NEW; 10 /* -------- aux stuff ---------- */ 75 lzw_enc_t *d = _new(lzw_enc_t, 512); 11 void* mem_alloc(size_t item_size, size_t n_item) 76 12 { 77 if (max_bits > 16) max_bits = 16; 13 size_t *x = calloc(1, sizeof(size_t)*2 + n_item * item_size); 78 if (max_bits < 9 ) max_bits = 12; 14 x[0] = item_size; 79 15 x[1] = n_item; 80 byte *out = _new(ushort, 4); 16 return x + 2; 81 int out_len = 0, o_bits = 0; 17 } 82 uint32_t tmp = 0; 18 83 19 void* mem_extend(void *m, size_t new_n) 84 inline void write_bits(ushort x) { 20 { 85 tmp = (tmp << bits) | x; 21 size_t *x = (size_t*)m - 2; 86 o_bits += bits; 22 x = realloc(x, sizeof(size_t) * 2 + *x * new_n); 87 if (_len(out) <= out_len) _extend(out); 23 if (new_n > x[1]) 88 while (o_bits >= 8) { 24 memset((char*)(x + 2) + x[0] * x[1], 0, x[0] * (new_n - x[1])); 89 o_bits -= 8; 25 x[1] = new_n; 90 out[out_len++] = tmp >> o_bits; 26 return x + 2; 91 tmp &= (1 << o_bits) - 1; 27 } 92 } 28 93 } 29 inline void _clear(void *m) 94 30 { 95 //write_bits(M_CLR); 31 size_t *x = (size_t*)m - 2; 96 for (code = *(in++); --len; ) { 32 memset(m, 0, x[0] * x[1]); 97 c = *(in++); 33 } 98 if ((nc = d[code].next[c])) 34 99 code = nc; 35 #define _new(type, n) mem_alloc(sizeof(type), n) 100 else { 36 #define _del(m) { free((size_t*)(m) - 2); m = 0; } 101 write_bits(code); 37 #define _len(m) *((size_t*)m - 1) 102 nc = d[code].next[c] = next_code++; 38 #define _setsize(m, n) m = mem_extend(m, n) 103 code = c; 39 #define _extend(m) m = mem_extend(m, _len(m) * 2) 104 } 40 105 41 106 /* next new code would be too long for current table */ 42 /* ----------- LZW stuff -------------- */ 107 if (next_code == next_shift) { 43 typedef uint8_t byte; 108 /* either reset table back to 9 bits */ 44 typedef uint16_t ushort; 109 if (++bits > max_bits) { 45 110 /* table clear marker must occur before bit reset */ 46 #define M_CLR 256 /* clear table marker */ 111 write_bits(M_CLR); 47 #define M_EOD 257 /* end-of-data marker */ 112 48 #define M_NEW 258 /* new code index */ 113 bits = 9; 49 114 next_shift = 512; 50 /* encode and decode dictionary structures. 115 next_code = M_NEW; 51 for encoding, entry at code index is a list of indices that follow current one, 116 _clear(d); 52 i.e. if code 97 is 'a', code 387 is 'ab', and code 1022 is 'abc', 117 } else /* or extend table */ 53 then dict[97].next['b'] = 387, dict[387].next['c'] = 1022, etc. */ 118 _setsize(d, next_shift *= 2); 54 typedef struct { 119 } 55 ushort next[256]; 120 } 56 } lzw_enc_t; 121 57 122 write_bits(code); 58 /* for decoding, dictionary contains index of whatever prefix index plus trailing 123 write_bits(M_EOD); 59 byte. i.e. like previous example, 124 if (tmp) write_bits(tmp); 60 dict[1022] = { c: 'c', prev: 387 }, 125 61 dict[387] = { c: 'b', prev: 97 }, 126 _del(d); 62 dict[97] = { c: 'a', prev: 0 } 127 63 the "back" element is used for temporarily chaining indices when resolving 128 _setsize(out, out_len); 64 a code to bytes 129 return out; 65 */ 130 } 66 typedef struct { Friday, 18 May 12
  • 51. 1 (defn make-dict [] 2 (let [vals (range 0 256)] 3 (zipmap (map (comp #'list #'char) vals) vals))) 4 5 (defn compress [#^String text] 6 (loop [t (seq text) 7 r '() 8 w '() 9 dict (make-dict) 10 s 256] 11 (let [c (first t)] 12 (if c 13 (let [wc (cons c w)] 14 (if (get dict wc) 15 (recur (rest t) r wc dict s) 16 (recur (rest t) (cons (get dict w) r) (list c) 17 (assoc dict wc s) (inc s)))) 18 (reverse (if w (cons (get dict w) r) r)))))) 19 20 (compress "TOBEORNOTTOBEORTOBEORNOT") Friday, 18 May 12
  • 53. 1 import Data.List 2 import Data.Char 3 import Data.Maybe 4 5 doLZW _ [] = [] 6 doLZW as (x:xs) = lzw (map return as) [x] xs 7 where lzw a w [] = [fromJust $ elemIndex w a] 8 lzw a w (x:xs) | w' `elem` a = lzw a w' xs 9 | otherwise = fromJust (elemIndex w a) : 10 lzw (a++[w']) [x] xs 11 where w' = w++[x] Friday, 18 May 12
  • 57. Closing Thought One thing that makes a programming language ‘more powerful’ in my opinion is the provision of more ways to factor programs. Or if you prefer, more axes of composition. The more different ways you can compose programs out of subprograms, the more powerful a language is. Reg Braithwaite - Functional Programming Matters Friday, 18 May 12
  • 58. Thanks! Andrew Jones @andrew_jones Thom Leggett @thomleggett Friday, 18 May 12