-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.xml
769 lines (616 loc) · 110 KB
/
feed.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>iOSDev.NYC</title>
<description>A place to go for all iOS developers</description>
<link>http://iosdev.nyc/</link>
<atom:link href="http://iosdev.nyc/feed.xml" rel="self" type="application/rss+xml" />
<item>
<title>GitHub Pages and Automatic Deployment</title>
<description><p>In this post, I&rsquo;ll help you set up automatic website deployment to free GitHub Pages hosting, using <a href="http://www.wercker.com">Wercker</a> (also free!).</p>
<p>Recently, my website was down for a few weeks, after I stopped <a href="https://www.bitballoon.com/pricing">BitBallon</a> subscription. This time I wanted to try one of the free alternatives.</p>
<p>I also wanted to learn something new in the process &ndash; that&rsquo;s why I didn&rsquo;t want to use GitHub for building my Jekyll blog and that&rsquo;s how I stumbled upon Wercker. </p>
<h1 id="build-your-website">Build your website</h1>
<p>I won&rsquo;t cover building your website in details here. This is something requiring a separate post, or better, a series of posts, so I hope a website is something you already have.</p>
<p>In my case, I&rsquo;m using <a href="https://jekyllrb.com">Jekyll</a> with <a href="https://github.com/johnotander/pixyll">Pixyll theme</a>, with a few minor changes.</p>
<p>If you&rsquo;re not a fan of Jekyll, here&rsquo;s a short list of some other frameworks I was considering:</p>
<ul>
<li><a href="http://wintersmith.io">http://wintersmith.io</a></li>
<li><a href="http://assemble.io">http://assemble.io</a></li>
<li><a href="https://www.gatsbyjs.org">https://www.gatsbyjs.org</a></li>
<li><a href="https://hexo.io">https://hexo.io</a></li>
<li><a href="http://gohugo.io">http://gohugo.io</a></li>
</ul>
<h1 id="github-pages">GitHub Pages</h1>
<p>To host your website on GitHub pages, the easiets way is to create a repo named <code>YOUR-GITHUB-USERNAME.github.io</code> and store your website on the master branch (you can <a href="https://github.com/MariuszWisniewski/MariuszWisniewski.github.io">see mine here</a>).</p>
<p>This is where Wercker will push your website after building it.</p>
<h1 id="wercker">Wercker</h1>
<h2 id="build-website-script">Build website script</h2>
<p>First, prepare a piece of code which you will use to build your website as if you were building it for the first time. It might be only a one-liner, just write it down somewhere as we will use it in a minute.</p>
<p>In my case (using Jekyll) it&rsquo;s:</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">bundle install
<span class="nv">JEKYLL_ENV</span><span class="o">=</span>production bundle <span class="nb">exec </span>jekyll build --trace
</code></pre></div>
<blockquote>
<p>I set jekyll environment variable here, which you might not need. My website behaves differently when it comes to analytics in development/production mode, so before building for deployment, I always switch to production.</p>
</blockquote>
<h2 id="add-wercker-app">Add Wercker app</h2>
<p>Create an account on <a href="http://www.wercker.com">Wercker website</a>. After you create it, it should ask you to create your first app. If it won&rsquo;t, use this <a href="https://app.wercker.com/applications/create">create new app link</a>.</p>
<p>Choose where you keep your website source code (GitHub or BitBucket) and which access configuration you want (I went with the default one).</p>
<p>Click finish (you don&rsquo;t have to make your app public) and continue to configuring your workflow.</p>
<p>You should end up on <code>Runs</code> tab, which will be asking you to add <code>wercker.yml</code> file to your repo.</p>
<p>Here&rsquo;s the one I used:</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">box: ruby
build:
steps:
<span class="c"># Go into site folder and install dependencies defined in Gemfile</span>
- script:
name: Install bundle
code: |
bundle install
<span class="c"># Set production environment and build</span>
<span class="c"># Generate the jekyll site with trace information on.</span>
<span class="c"># And store the result in the $WERCKER_OUTPUT_DIR, the</span>
<span class="c"># directory that is shared with the deployment pipeline.</span>
- script:
name: Build Jekyll Page
code: |
<span class="nv">JEKYLL_ENV</span><span class="o">=</span>production bundle <span class="nb">exec </span>jekyll build --trace --destination <span class="nv">$WERCKER_OUTPUT_DIR</span>
deploy:
steps:
- add-ssh-key:
keyname: SSH_DEPLOY
host: github.com
- add-to-known_hosts:
hostname: github.com
fingerprint: 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
- leipert/[email protected]:
host: github.com
repo: username/username.github.io
branch: master
basedir: .
</code></pre></div>
<p>(you can also <a href="wercker.yml">download the file here</a>)</p>
<h3 id="wercker-configuration">Wercker configuration</h3>
<p>Let&rsquo;s see how is the <code>wercker.yml</code> config file built. </p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">box: ruby
</code></pre></div>
<p><a href="http://devcenter.wercker.com/docs/boxes">Box</a> defines the name of the image that will be used for building your website. You can use any Docker image here, I went with standard <code>ruby</code> image.</p>
<p>We have two major steps:</p>
<ul>
<li>build</li>
<li>deploy</li>
</ul>
<p>They are connected to Pipelines, which you can find in Wercker under Workflows tab.</p>
<p><img src="/images/posts/2017-11-20-wercker-github-pages/workflow_tab.png" alt="Workflows Tab"></p>
<p>As you can see on the website, you have only <code>build</code> pipeline in there. Scroll down and click <code>Add new pipeline</code>.</p>
<p><img src="/images/posts/2017-11-20-wercker-github-pages/new-pipeline.png" alt="Add new pipeline"></p>
<ul>
<li><code>Name</code> &ndash; Put here whatever you think best describes deployment phase;</li>
<li><code>YML Pipeline name</code> &ndash; This has to be the name of the pipeline as defined in <code>wercker.yml</code> file. In the file I provided it&rsquo;s called <code>deploy</code> so put the same name in here. If you&rsquo;d like to name it differently you can do so, just remember to also change pipeline name in <code>yml</code> file;</li>
<li><code>Hook type</code> &ndash; choose <code>default</code> which will allow chaining this pipeline after <code>build</code> phase.</li>
</ul>
<p>Go back to Workflows tab and click plus button next to the <code>build</code> pipeline in the Editor.</p>
<p><img src="/images/posts/2017-11-20-wercker-github-pages/plus-button-build.png" alt="Build plus button"></p>
<p>The only thing you should change now is <code>Execute pipeline</code> &ndash; click on the field, select <code>deploy</code> and confirm by clicking <code>Add</code>.</p>
<p><img src="/images/posts/2017-11-20-wercker-github-pages/new-pipeline-workflow.png" alt="Add deploy pipeline to Workflow"></p>
<p>This assures that our website is deployed automatically to GitHub Pages repo after each build.</p>
<h3 id="build-pipeline">Build pipeline</h3>
<p><em>(I removed comments from the file below for clarity)</em></p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">build:
steps:
- script:
name: Install bundle
code: |
bundle install
- script:
name: Build Jekyll Page
code: |
<span class="nv">JEKYLL_ENV</span><span class="o">=</span>production bundle <span class="nb">exec </span>jekyll build --trace --destination <span class="nv">$WERCKER_OUTPUT_DIR</span>
</code></pre></div>
<p>As you can see, build pipeline steps are quite simple.</p>
<p>We use custom scripts which consist of two parts &ndash; script name that will be displayed on Wercker website during each pipeline phase and the code that will be executed.</p>
<p>We have two scripts that could be easily combined into one, but in case of failure you will see what failed faster with separate steps.<br>
I used the script I asked you to prepare before, with just one small change &ndash; as a build destination, we use <code>$WERCKER_OUTPUT_DIR</code>, a variable that defines a folder that can be accessed also from <code>deploy</code> pipeline.</p>
<p>If you use anything else than Jekyll &ndash; replace jekyll build command with anything else that builds your website. Just remember to use <code>$WERCKER_OUTPUT_DIR</code> as a destination for the build process.</p>
<h3 id="deploy-pipeline">Deploy pipeline</h3>
<p>We will start from creating an <code>ssh key</code> for deploying your website to GitHub pages.</p>
<p>Go into Environment tab and click on <code>Generate SSH Key</code>.</p>
<p><img src="/images/posts/2017-11-20-wercker-github-pages/environment-tab.png" alt="Environment tab"></p>
<p><img src="/images/posts/2017-11-20-wercker-github-pages/generate-ssh-key.png" alt="Generate SSH Key"></p>
<p>As SSH Key name use <code>SSH_DEPLOY</code> (the same name we used in <code>yml</code> file), and click <code>Generate</code>.</p>
<p><img src="/images/posts/2017-11-20-wercker-github-pages/generate-ssh-key-name.png" alt="New SSH Key"></p>
<p>Now you should see two positions under Environment tab:</p>
<ul>
<li><code>SSH_DEPLOY_PUBLIC</code></li>
<li><code>SSH_DEPLOY_PRIVATE</code></li>
</ul>
<p>Copy value under <code>SSH_DEPLOY_PUBLIC</code> (it starts with <code>ssh-rsa</code>) and go to your GitHub Pages repo prepared before (the one named <code>YOUR-GITHUB-USERNAME.github.io</code>).</p>
<p>Go into repository Settings and then to <code>Deploy keys</code>.</p>
<p><img src="/images/posts/2017-11-20-wercker-github-pages/github-go-to-deploy-keys.png" alt="Go to GitHub Deploy Keys"></p>
<p>Add a new deploy key &ndash; you can name it however you want, I used <code>wercker-deploy-key</code> so I can remember later what it was created for. <br>
Remember to check <code>Allow write access</code> box so Wercker can push to this repository. Into <code>Key</code> field paste the value of <code>SSH_DEPLOY_PUBLIC</code> key you copied from Wercker website.</p>
<p><img src="/images/posts/2017-11-20-wercker-github-pages/paste-wercker-deploy-key.png" alt="Add new deploy key"></p>
<p>Let&rsquo;s get back to our <code>wercker.yml</code> config file.</p>
<div class="highlight"><pre><code class="language-bash" data-lang="bash">deploy:
steps:
- add-ssh-key:
keyname: SSH_DEPLOY
host: github.com
- add-to-known_hosts:
hostname: github.com
fingerprint: 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
- leipert/[email protected]:
host: github.com
repo: username/username.github.io
branch: master
basedir: .
</code></pre></div>
<p>We have 3 steps in here:</p>
<ol>
<li>Adding an SSH key that will be used to push to your repo. We already created this key and added it to GitHub repository settings, so Wercker will be able to use it for pushing your website;</li>
<li>Add to known hosts so Wercker environment will trust <code>github.com</code> host</li>
<li><a href="https://app.wercker.com/applications/53f202b50705e3656b000033/tab/details/">Git-push</a> (<a href="https://github.com/leipert/step-git-push">also on GitHub</a>). All you have to do is replace <code>username</code> with your actual username on GitHub. In my case this step looks like this:</li>
</ol>
<div class="highlight"><pre><code class="language-bash" data-lang="bash"> - leipert/[email protected]:
host: github.com
repo: MariuszWisniewski/MariuszWisniewski.github.io
branch: master
basedir: .
</code></pre></div>
<h1 id="summary">Summary</h1>
<p>That&rsquo;s it! </p>
<p>Now you can push a new commit to the repository where you host source code for your website (the one you linked in the <a href="#add-wercker-app">Add Wercker app</a> step).</p>
<p>Each new commit to this repo will build your website using the script provided in <code>build</code> pipeline and will push it to GitHub Pages repository using <code>deploy</code> pipeline.</p>
<p>Let me know if you have any questions. In case of doubts, feel free to leave a comment!</p>
</description>
<pubDate>Mon, 20 Nov 2017 04:00:00 -0500</pubDate>
<link>http://iosdev.nyc/blog/wercker-github-pages/</link>
<guid isPermaLink="true">http://iosdev.nyc/blog/wercker-github-pages/</guid>
</item>
<item>
<title>Credit Card Validation</title>
<description><p>When working on app recently, our payment processing provider, communicated to us that unfortunately they support only certain kinds of credit cards.</p>
<p>We didn&rsquo;t want to let users type card number that is not supported, only to send it to server, and after getting the response, show an error. </p>
<p>If we know what types are supported, we can show the error earlier, and avoid sending any data &ndash; making the experience much better for the user.</p>
<p>We tried several frameworks but what we found is that:</p>
<ol>
<li>most of the frameworks allow passing not correct CC number (e.g. Visa with 11 digits only validates just fine)</li>
<li>all of the frameworks we tested, allow checking credit card type only after all digits are passed</li>
</ol>
<p>Especially 2. was imoportant for us to improve on. If we know e.g. that all Visa cards start with number 4, why wait until all 16 digits are typed?</p>
<p>Same goes for cards that are not supported. If user types 34 in CC number field, we know it will be American Express card &ndash; and if we don&rsquo;t support it, we can immediately show the error.</p>
<p>I described in more details what I created in a <a href="https://medium.com/digitalforms/do-you-validate-credit-cards-in-your-ios-app-74d21e4f97a8">Medium blog post</a>, so please go there for more information. </p>
<p>Or you can go directly to our <a href="https://github.com/DigitalForms/CCValidator">CCValidator framework GitHub page</a>, and read installation and usage guide, or read short description below.</p>
<h3 id="installation-and-usage-guide">Installation and usage guide</h3>
<p><a href="https://github.com/DigitalForms/CCValidator">CCValidator</a> is available on <a href="https://cocoapods.org/pods/CCValidator">CocoaPods</a> already.</p>
<p>To install it, add to your Podfile:</p>
<div class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">pod</span> <span class="s1">'CCValidator'</span><span class="p">,</span> <span class="s1">'~&gt; 1.0'</span>
</code></pre></div>
<p>Then, use it in your code:</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="k">let</span> <span class="nv">recognizedType</span> <span class="o">=</span> <span class="kt">CCValidator</span><span class="o">.</span><span class="nf">typeCheckingPrefixOnly</span><span class="p">(</span><span class="nv">creditCardNumber</span><span class="p">:</span> <span class="n">numberAsString</span><span class="p">)</span>
<span class="c1">//check if type is e.g. .Visa, .MasterCard or .NotRecognized</span>
</code></pre></div>
<p>or if you don&rsquo;t need credit card type, and need only to validate CC number correctness, use:</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="k">let</span> <span class="nv">numberAsString</span> <span class="o">=</span> <span class="n">textField</span><span class="o">.</span><span class="n">text</span>
<span class="k">let</span> <span class="nv">isFullCardDataOK</span> <span class="o">=</span> <span class="kt">CCValidator</span><span class="o">.</span><span class="nf">validate</span><span class="p">(</span><span class="nv">creditCardNumber</span><span class="p">:</span> <span class="n">numberAsString</span><span class="p">)</span>
</code></pre></div>
<p>Hope that&rsquo;s gonna be useful for you. We will gladly welcome all contributions and ideas for what we should add to it!</p>
<p><a href="https://github.com/DigitalForms/CCValidator">https://github.com/DigitalForms/CCValidator</a></p>
</description>
<pubDate>Wed, 22 Feb 2017 04:00:00 -0500</pubDate>
<link>http://iosdev.nyc/blog/credit-card-validation/</link>
<guid isPermaLink="true">http://iosdev.nyc/blog/credit-card-validation/</guid>
</item>
<item>
<title>Swifty function currying</title>
<description><p>Function currying is something I wanted to dig into for some time now, and finally I found a good time for it, especially now, after some <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0002-remove-currying.md">changes introduced to the syntax</a> in Swift 3. </p>
<h3 id="what-is-function-currying">What is function currying?</h3>
<p>What is <code>function currying</code> (in general and in Swift)?</p>
<p>If you want to get a full definition, see <a href="https://en.wikipedia.org/wiki/Currying">Wikipedia</a>. In short, function currying lets us write code that translates a function taking two arguments, into a function taking one argument, and returning a function taking the next argument (and so on for 2, 3, 4, &hellip; arguments).</p>
<p>It might not sound super clear yet, so let&rsquo;s take a look at some examples.</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">multiplyTwoNumbers</span><span class="p">(</span><span class="n">_</span> <span class="nv">a</span><span class="p">:</span> <span class="kt">Int</span><span class="p">,</span> <span class="n">_</span> <span class="nv">b</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">*</span> <span class="n">b</span>
<span class="p">}</span>
<span class="nf">multiplyTwoNumbers</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="c1">//result: 6</span>
</code></pre></div>
<p>Super simple, takes two Integers and returns result of their multiplication. What we have here is a 2 argument function that we will transform (as in definition), into function taking only one argument, returning another function, taking the second argument and returning an Integer.</p>
<p>In pseudo-code it would be something like:</p>
<div class="highlight"><pre><code class="language-" data-lang="">function(a, b) -&gt; c
//into
function(a) -&gt; (function(b) -&gt; c)
</code></pre></div>
<p>You could ask - how is that useful? Well, it allows for this newly created (curried) function to be evaluated over a period of time, with each call passing only a subset of the arguments (and saving each call result on the way if needed).</p>
<p>So looking at our previous example, we could translate it into curried version like this:</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">multiply</span><span class="p">(</span><span class="n">_</span> <span class="nv">a</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">{</span> <span class="n">b</span> <span class="k">in</span> <span class="n">a</span> <span class="o">*</span> <span class="n">b</span> <span class="p">}</span>
<span class="p">}</span>
<span class="nf">multiply</span><span class="p">(</span><span class="mi">2</span><span class="p">)(</span><span class="mi">3</span><span class="p">)</span> <span class="c1">//result: 6</span>
</code></pre></div>
<p>At this point, there&rsquo;s still no real advantage, and we have this funky syntax, which is not obvious when you see it for the first time. But like I mentioned earlier, it allows us to evaluate it over time, and save each call result on the way. </p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="k">let</span> <span class="nv">multiplyBy10</span> <span class="o">=</span> <span class="nf">multiply</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="c1">//result: function that will multiply its argument by 10</span>
<span class="k">let</span> <span class="nv">multiplyBy5</span> <span class="o">=</span> <span class="nf">multiply</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="c1">//result: function that will multiply its argument by 5</span>
<span class="k">let</span> <span class="nv">multiplyBy2</span> <span class="o">=</span> <span class="nf">multiply</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="c1">//result: function that will multiply its argument by 2</span>
</code></pre></div>
<p>Now you can call it like any other function:</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="nf">multiplyBy10</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1">//result: 20</span>
<span class="k">let</span> <span class="nv">y</span> <span class="o">=</span> <span class="nf">multiplyBy5</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1">//result: 10</span>
<span class="k">let</span> <span class="nv">z</span> <span class="o">=</span> <span class="nf">multiplyBy2</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1">//result: 4</span>
</code></pre></div>
<p>If we wanted to achieve same thing without currying, we would have to write a new function, for every first argument combination, like this:</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">multiplyBy10</span><span class="p">(</span><span class="n">_</span> <span class="nv">a</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">*</span> <span class="mi">10</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">multiplyBy5</span><span class="p">(</span><span class="n">_</span> <span class="nv">a</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">*</span> <span class="mi">5</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">multiplyBy2</span><span class="p">(</span><span class="n">_</span> <span class="nv">a</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">*</span> <span class="mi">2</span>
<span class="p">}</span>
</code></pre></div>
<p>Much more writing, error-prone (most probably would implement it by copy-pasting) and when you know what function currying is, there&rsquo;s no real difference in readablity (in my opinion).</p>
<h3 id="transforming-standard-functions-into-their-curried-versions">Transforming standard functions into their curried versions</h3>
<p>So far, we&rsquo;ve been able to transform our own two-argument function into curried one. That&rsquo;s quite easy, as we just rewrote it from scratch. Lets try to do something more complicated - tranform in a similar way UIColor.init(red: green: blue: alpha:) function, which we cannot modify directly in the source code.</p>
<p>How can we create curried function out of one that&rsquo;s not curried? We need to implement a function that takes an existing function as a parameter (the one we want to transform) and returns a curried wrapper around it.</p>
<p>Before we go with UIColor&rsquo;s init function, we will start by transforming our own multiplying function. To do this, we will create a function that takes (Int,Int)-&gt;Int function as an argument and returns a curried wrapper around it.</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">curriedVersion</span><span class="p">(</span><span class="nv">f</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">Int</span><span class="p">,</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">{</span> <span class="n">a</span> <span class="k">in</span> <span class="p">{</span> <span class="n">b</span> <span class="k">in</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<hr>
<p><em>(What is <code>@escaping</code> keyword? For more info I encourage you to visit <a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html">Apple docs</a>, but in short &ndash; we use it to mark a closure that will not be invoked directly by function taking this closure as an argument. In our case, we don&rsquo;t call <code>f</code> function directly, we just pass it around and return it to be called later)</em></p>
<hr>
<p>As we see, our <code>curriedVersion</code> function, takes as an argument a function (that matches <code>multiplyTwoNumbers</code> signature) and returns a function, that returns a function, that returns an Integer (I know it might be hard to follow, but bare with me).</p>
<p>Originally, our curried function <code>multiply</code> returned a function, that took one argument and returned an Integer. Because we&rsquo;re transforming a function here, we need a wrapper around it. Let&rsquo;s take a look again at pseudo-code:</p>
<p><strong>Standard function</strong> taking two arguments and returning a number</p>
<div class="highlight"><pre><code class="language-" data-lang="">standardFunction = function(a,b) -&gt; c
</code></pre></div>
<p><strong>Curried function</strong> that takes one argument and returns a function taking second argument, and returning a number</p>
<div class="highlight"><pre><code class="language-" data-lang="">curriedFunction = function(a) -&gt; (function(b) -&gt; c)
</code></pre></div>
<p>How do we make <code>curriedFunction</code> out of <code>standardFunction</code>? We create a wrapper around it</p>
<div class="highlight"><pre><code class="language-" data-lang="">transformingFunction = function(standardFunction) -&gt; curriedFunction
</code></pre></div>
<p>If we now substitute function names with their definitions we get</p>
<div class="highlight"><pre><code class="language-" data-lang="">transformingFunction = function(function(a,b) -&gt; c) -&gt; function(a) -&gt; (function(b) -&gt; c)
</code></pre></div>
<p>which looks similar to</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">curriedVersion</span><span class="p">(</span><span class="nv">f</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">Int</span><span class="p">,</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
</code></pre></div>
<p>Ok, so how do we use our transforming function? We just call it by passing to it our old <code>multiplyTwoNumbers</code> function.</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="k">let</span> <span class="nv">curriedMultiply</span> <span class="o">=</span> <span class="nf">curriedVersion</span><span class="p">(</span><span class="nv">f</span><span class="p">:</span> <span class="n">multiplyTwoNumbers</span><span class="p">)</span>
<span class="nf">curriedMultiply</span><span class="p">(</span><span class="mi">2</span><span class="p">)(</span><span class="mi">3</span><span class="p">)</span>
</code></pre></div>
<h3 id="transforming-uicolor-init-into-curried-function">Transforming <code>UIColor.init()</code> into curried function</h3>
<p>Ok, now that we successfully transformed a 2 arguments function into its curried version, let&rsquo;s go back to our goal - transforming <code>UIColor.init(red: green: blue: alpha:)</code> function that takes 4 arguments (as I mentioned earlier &ndash; same can be applied to any 2+ argument function).</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">transformColorInit</span><span class="p">(</span><span class="nv">f</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">CGFloat</span><span class="p">,</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="kt">CGFloat</span><span class="p">,</span> <span class="kt">CGFloat</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">UIColor</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">((</span><span class="kt">CGFloat</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">CGFloat</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">CGFloat</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">CGFloat</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">UIColor</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">{</span> <span class="n">red</span> <span class="k">in</span> <span class="p">{</span> <span class="n">green</span> <span class="k">in</span> <span class="p">{</span> <span class="n">blue</span> <span class="k">in</span> <span class="p">{</span> <span class="n">alfa</span> <span class="k">in</span> <span class="nf">f</span><span class="p">(</span><span class="n">red</span><span class="p">,</span> <span class="n">green</span><span class="p">,</span> <span class="n">blue</span><span class="p">,</span> <span class="n">alfa</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Might look hard to read, but it&rsquo;s quite simple - our <code>transformColorInit</code> takes as an argument function that matches the signature of <code>UIColor.init(red: green: blue: alpha:)</code> and it returns a function that takes a float and returns a function, that takes a float and returns a function, that takes a float and returns a function, that takes a float and returns a color.</p>
<p>General rule is &ndash; you strip away one argument at a time. <br>
If your initial function takes 2 arguments - you will have <code>function returning a function</code> 2 times. <br>
If it takes 4 arguments (like <code>UIColor.init(...)</code>), you will have <code>function returning a function</code> 4 times etc.<br>
Other than the syntax of it looking a bit weird, I think it should be quite easy to understand.</p>
<p>How do we use our transformer now? As before, we just call it, by passing to it <code>UIColor.init(...)</code> function.</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="k">let</span> <span class="nv">curriedInit</span> <span class="o">=</span> <span class="nf">transformColorInit</span><span class="p">(</span><span class="nv">f</span><span class="p">:</span> <span class="kt">UIColor</span><span class="o">.</span><span class="nf">init</span><span class="p">(</span><span class="nv">red</span><span class="p">:</span> <span class="nv">green</span><span class="p">:</span> <span class="nv">blue</span><span class="p">:</span> <span class="nv">alpha</span><span class="p">:))</span>
</code></pre></div>
<p><em>(notice that we need to pass full signature of <code>init</code> function &ndash; it&rsquo;s because <code>UIColor</code> has more than 1 <code>init</code> function taking 4 <code>CGFloat</code> arguments)</em></p>
<p>Now we can e.g. create a function, that will be creating red color with different values of alpha channel (passed as an argument).</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="k">let</span> <span class="nv">createRedWithAlfa</span> <span class="o">=</span> <span class="nf">curriedInit</span><span class="p">(</span><span class="mi">1</span><span class="p">)(</span><span class="mi">0</span><span class="p">)(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">let</span> <span class="nv">red05</span> <span class="o">=</span> <span class="nf">createRedWithAlfa</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span>
<span class="k">let</span> <span class="nv">red02</span> <span class="o">=</span> <span class="nf">createRedWithAlfa</span><span class="p">(</span><span class="mf">0.2</span><span class="p">)</span>
<span class="k">let</span> <span class="nv">red09</span> <span class="o">=</span> <span class="nf">createRedWithAlfa</span><span class="p">(</span><span class="mf">0.9</span><span class="p">)</span>
</code></pre></div>
<p>Of course, you&rsquo;re not limited to just last argument, you could also make a function taking 2 or 3 arguments if you needed to:</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="k">let</span> <span class="nv">createBlueColorWithAlpha</span> <span class="o">=</span> <span class="nf">curriedInit</span><span class="p">(</span><span class="mi">0</span><span class="p">)(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">let</span> <span class="nv">semiTransparentBlue</span> <span class="o">=</span> <span class="nf">createBlueColorWithAlpha</span><span class="p">(</span><span class="mi">1</span><span class="p">)(</span><span class="mf">0.5</span><span class="p">)</span>
<span class="k">let</span> <span class="nv">solidBlue</span> <span class="o">=</span> <span class="nf">createBlueColorWithAlpha</span><span class="p">(</span><span class="mi">1</span><span class="p">)(</span><span class="mi">1</span><span class="p">)</span>
</code></pre></div>
<h3 id="generic-tranforming-function">Generic tranforming function</h3>
<p>Do we need to write a different transforming function for every function that we want to transform? Fortunately, the answer is <code>NO</code>.</p>
<p>We can use the power of Swift&rsquo;s generics and write a generic function, that will take as an argument function with non-specified argument types (can be Int, Float, String, Class type etc) and return its curried version.</p>
<p>For 4 argument function (like <code>UIColor.init(...)</code>) it could look like this:</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">public</span> <span class="kd">func</span> <span class="n">curry</span><span class="o">&lt;</span><span class="kt">A</span><span class="p">,</span> <span class="kt">B</span><span class="p">,</span> <span class="kt">C</span><span class="p">,</span> <span class="kt">D</span><span class="p">,</span> <span class="kt">E</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span> <span class="nv">function</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">A</span><span class="p">,</span> <span class="kt">B</span><span class="p">,</span> <span class="kt">C</span><span class="p">,</span> <span class="kt">D</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">E</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">B</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">C</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">D</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">E</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">{</span> <span class="p">(</span><span class="nv">a</span><span class="p">:</span> <span class="kt">A</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">B</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">C</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">D</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">E</span> <span class="k">in</span> <span class="p">{</span> <span class="p">(</span><span class="nv">b</span><span class="p">:</span> <span class="kt">B</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">C</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">D</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">E</span> <span class="k">in</span> <span class="p">{</span> <span class="p">(</span><span class="nv">c</span><span class="p">:</span> <span class="kt">C</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">(</span><span class="kt">D</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">E</span> <span class="k">in</span> <span class="p">{</span> <span class="p">(</span><span class="nv">d</span><span class="p">:</span> <span class="kt">D</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">E</span> <span class="k">in</span> <span class="nf">function</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>It is a generic function with 5 different placeholder types &ndash; 4 of them for arguments of passed function and 1 for its return type. Of course, nothing stops us from passing a function that has all 4 arguments of the same type.</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="k">let</span> <span class="nv">createRedWithAlfaV2</span> <span class="o">=</span> <span class="nf">curry</span><span class="p">(</span><span class="kt">UIColor</span><span class="o">.</span><span class="nf">init</span><span class="p">(</span><span class="nv">red</span><span class="p">:</span> <span class="nv">green</span><span class="p">:</span> <span class="nv">blue</span><span class="p">:</span> <span class="nv">alpha</span><span class="p">:))(</span><span class="mi">1</span><span class="p">)(</span><span class="mi">0</span><span class="p">)(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">let</span> <span class="nv">red01</span> <span class="o">=</span> <span class="nf">createRedWithAlfaV2</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
</code></pre></div>
<p>Code for <code>curry</code> function was written by guys from <a href="https://github.com/thoughtbot/Curry/blob/master/Source/Curry.swift">thoughbot</a> &ndash; feel free to take a look and see an example for function transforming a function taking 20 arguments :)</p>
<hr>
<p>That&rsquo;s it for today. You should know what function currying is, how to use it and how to transform any 2+ argument function into its curried version. If you have any questions &ndash; let me know in comments!</p>
<hr>
<p>Sources (I recommend you to read first 3 positions, they did help me to understand better what function currying is!):</p>
<ul>
<li><a href="https://robots.thoughtbot.com/introduction-to-function-currying-in-swift">thoughtbot Blog</a></li>
<li><a href="https://yeti.co/blog/using-curried-functions-in-swift/">Yeti.co Blog</a></li>
<li><a href="https://otters.io/a-little-less-spice-in-swift-3">Otters.io Blog</a></li>
<li><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0002-remove-currying.md">Proposal for removing currying <code>func</code> declaration syntax in Swift 3</a></li>
<li><a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html">Apple Closure docs</a></li>
</ul>
</description>
<pubDate>Fri, 10 Feb 2017 11:00:00 -0500</pubDate>
<link>http://iosdev.nyc/blog/function-currying-swift/</link>
<guid isPermaLink="true">http://iosdev.nyc/blog/function-currying-swift/</guid>
</item>
<item>
<title>Pyramid of Doom Updated (Swift 3)</title>
<description><p>Since last posts about using <code>let</code> and <code>guard</code>, Swift 3 came out and changed a few things here and there. Let&rsquo;s see what&rsquo;s new! </p>
<p>Let&rsquo;s start from the beginning - the basics are still the same. So still, please don&rsquo;t do:</p>
<div class="highlight"><pre><code class="language-" data-lang="">if let x = x {
if let y = y {
if let z = z {
print("x = \(x), y = \(y), z = \(z)")
x * y * z * 3
}
}
}
</code></pre></div>
<p>or</p>
<div class="highlight"><pre><code class="language-" data-lang="">func objcStyle(foo: Int?, bar: Int?) {
if (foo == nil || bar == nil) {
return
}
let a = foo!
let b = bar!
a.advanced(by: b)
// or force unwrap both directly with
// a!.advanceBy(by: b)
}
</code></pre></div>
<p>and instead do these two:</p>
<div class="highlight"><pre><code class="language-" data-lang="">if let x = x, let y = y, let z = z {
print("x = \(x), y = \(y), z = \(z)")
x * y * z * 3
}
</code></pre></div>
<p>and</p>
<div class="highlight"><pre><code class="language-" data-lang="">func swiftGuardStyle(foo: Int?, bar: Int?) {
guard let a = foo, let b = bar else {
return
}
a.advanced(by: b)
}
</code></pre></div>
<p>In short - use Swift like it was meant to be, not like you would be writing Obj-C, jus using Swift syntax.</p>
<p>One important change is using <code>where</code> clause. For full summary of changes, you can visit <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0099-conditionclauses.md">Swift Evolution</a>.</p>
<p>In short - now, this change makes each clause in <code>if</code> statement separate from each other and limits clauses to a single item. To make it easier to understand, take a look at Swift 2 and 3 examples below:</p>
<div class="highlight"><pre><code class="language-" data-lang="">// SWIFT 2
func swiftGuardStyleWhere(book: Book?, author: Author?, rating: Int) {
guard rating &gt; 4, let book = book, author = author where author == "Bob" else {
return
}
// will only execute if book and author are non nil
// and when author's name is "Bob"
// and when rating is &gt; 4
doSomething()
}
</code></pre></div><div class="highlight"><pre><code class="language-" data-lang="">func swiftGuardStyleWhere(book: Book?, author: Author?, rating: Int) {
guard rating &gt; 4, let book = book, let author = author, author == "Bob" else {
return
}
// will only execute if book and author are non nil
// and when author's name is "Bob"
// and when rating is &gt; 4
}
</code></pre></div>
<p>What&rsquo;s different?</p>
<ul>
<li>Because each clause is now separated by <code>,</code>, you need to type <code>let</code> in each clause &ndash; before all confitional binding statements (it doesn&rsquo;t propagate)</li>
<li><code>where</code> keyword is removed and replaced by <code>,</code> &ndash; each sub-clause of the <code>if</code> statement is now an independent boolean test</li>
</ul>
<p>That&rsquo;s really it &ndash; when it comes to topics covered by previous two blog posts, nothing else changed. Make sure to get rid of <code>where</code> clause from your <code>if</code> statements and repeat <code>let</code> for every sub-clause and you will be fine!</p>
</description>
<pubDate>Tue, 07 Feb 2017 01:00:00 -0500</pubDate>
<link>http://iosdev.nyc/blog/let-guard-pyramid-of-doom-swift-3/</link>
<guid isPermaLink="true">http://iosdev.nyc/blog/let-guard-pyramid-of-doom-swift-3/</guid>
</item>
<item>
<title>Dealing with Swift's Pyramid of Doom</title>
<description><p><strong>Update, 07 Feb 2017</strong><br>
As of Swift 3, some of the code below may not work properly anymore. To see what changed, read <a href="/blog/let-guard-pyramid-of-doom-swift-3/">Pyramid of Doom Updated (Swift 3)</a>.</p>
<hr>
<p><strong>Original Post</strong></p>
<p>Today we continue the topic of avoiding Swift’s <a href="http://blog.scottlogic.com/2014/12/08/swift-optional-pyramids-of-doom.html"><em>Pyramid of Doom</em></a> that we started in <a href="/blog/let-guard/">previous post</a>, on <code>guard</code> statement. <br>
This time, we cover a feature from Swift 1.2 – it’s not a new thing, but still very handy and useful to have in your arsenal.</p>
<p>Every now and then I see older code that does something like this:</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">pyramidOfDoom</span><span class="p">(</span><span class="nv">x</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">y</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">z</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?)</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="n">x</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">y</span> <span class="o">=</span> <span class="n">y</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">z</span> <span class="o">=</span> <span class="n">z</span> <span class="p">{</span>
<span class="c1">//do something with x, y and z</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>It works and was necessary in Swift 1.0 (unless you added your own workaround for it).</p>
<p>In Swift 1.2, <code>if let</code> allows unwrapping multiple optionals, which lets us rewrite previous example into</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">noPyramid</span><span class="p">(</span><span class="nv">x</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">y</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">z</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?)</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">z</span> <span class="p">{</span>
<span class="c1">//do something with x, y and z</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>So much better!</p>
<p>What’s also useful and helpful for keeping your code clean, is the ability to add <code>where</code> clause to your optional binding. <br>
It lets you check not only if optionals hold any value, but also if that value meets certain condition.</p>
<p>Let’s take a look at code we would write normally, using <code>if</code> statement</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">ifStatement</span><span class="p">(</span><span class="nv">x</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">y</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">z</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?)</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">z</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">z</span> <span class="o">&lt;</span> <span class="mi">4</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">//do something with x, y and z</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Assuming we need <code>z</code> to be less than <code>4</code> - code seems pretty normal. Let’s rewrite it to use <code>where</code> clause</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">whereClause</span><span class="p">(</span><span class="nv">x</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">y</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">z</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?)</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">z</span> <span class="k">where</span> <span class="n">z</span> <span class="o">&lt;</span> <span class="mi">4</span> <span class="p">{</span>
<span class="c1">//do something with x, y and z</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>One less level of code indentation and a bit cleaner code. You can add one where clause per each <code>let</code> statement</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">multipleWhereClause</span><span class="p">(</span><span class="nv">x</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">y</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">z</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?)</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="n">x</span> <span class="k">where</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">,</span>
<span class="k">let</span> <span class="nv">y</span> <span class="o">=</span> <span class="n">y</span> <span class="k">where</span> <span class="n">y</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">,</span>
<span class="k">let</span> <span class="nv">z</span> <span class="o">=</span> <span class="n">z</span> <span class="k">where</span> <span class="n">z</span> <span class="o">&lt;</span> <span class="mi">4</span> <span class="p">{</span>
<span class="c1">//do something with x, y and z</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>which acts exactly the same as</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">multipleWhereClause2</span><span class="p">(</span><span class="nv">x</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">y</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?,</span> <span class="nv">z</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?)</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">z</span> <span class="k">where</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="mi">2</span> <span class="o">&amp;&amp;</span> <span class="n">y</span> <span class="o">&lt;</span> <span class="mi">3</span> <span class="o">&amp;&amp;</span> <span class="n">z</span> <span class="o">&lt;</span> <span class="mi">4</span> <span class="p">{</span>
<span class="c1">//do something with x, y and z</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>Which version to use? It’s mostly a matter of preference and depends what you think is easier to read.</p>
<p>I prefer version #1, as it’s a bit more explicit and for the same reason I advise you do the same – if you can add more readability by spending few more seconds on typing additional <code>let</code> statements – do so, people reading your code later will be grateful.</p>
<p>One important note to remember, is that assignment and unwrapping happens before the condition in <code>where</code> clause is checked. <br>
This means, if your condition is unrelated to unwrapped variables – it might be better to check the condition first. Why?</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">expensiveFunction</span><span class="p">(</span><span class="nv">startWith</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span><span class="p">?</span> <span class="p">{</span>
<span class="c1">//despite what you see here</span>
<span class="c1">//it is really expensive to call this function!</span>
<span class="k">return</span> <span class="n">startWith</span> <span class="o">*</span> <span class="mi">2</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">isTodayThursday</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Bool</span> <span class="p">{</span>
<span class="c1">//I'm writing it on Tuesday, so we can optimize ;)</span>
<span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">expensiveUnwrapping</span><span class="p">()</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="nf">expensiveFunction</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="n">y</span> <span class="o">=</span> <span class="nf">expensiveFunction</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="k">where</span> <span class="nf">isTodayThursday</span><span class="p">()</span> <span class="p">{</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>It matters how do we get our optional values. If, as in the example above, they come from a function that is really expensive to call – it would be smart to call it only if we can be sure that returned value will actually be used.</p>
<p>In the example above, our expensive function is being called twice, even though we don’t even get to use <code>x</code> and <code>y</code> variables.</p>
<p>For similar cases, you might use the fact that we are allowed to use one logic statement (outside of <code>where</code> clause) as long as it’s the first clause in the <code>if let</code> statement</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">unexpensiveUnwrapping</span><span class="p">()</span> <span class="p">{</span>
<span class="k">if</span> <span class="nf">isTodayThursday</span><span class="p">(),</span> <span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="nf">expensiveFunction</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="n">y</span> <span class="o">=</span> <span class="nf">expensiveFunction</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="p">{</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>This way our expensive functions will be called only if our initial logic clause is <code>true</code> (according to answer by Chris Lattner on <a href="https://groups.google.com/forum/#!topic/swift-language/TKQ2IOXR6AQ">google groups</a>).</p>
<p>Adding one logic clause at the beginning, works exactly the same with <code>guard</code> statement</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">guardUnexpensive</span><span class="p">()</span> <span class="p">{</span>
<span class="k">guard</span> <span class="nf">isTodayThursday</span><span class="p">(),</span> <span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="nf">expensiveFunction</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="n">y</span> <span class="o">=</span> <span class="nf">expensiveFunction</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="nf">print</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div>
<p>That’s it for now. In one of the next articles I will go into pattern matching and I’ll explain why following statements produce exactly same results. Stay tuned!</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="k">if</span> <span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="n">x</span> <span class="p">{</span>
<span class="nf">printSomething</span><span class="p">()</span>
<span class="p">}</span>
<span class="k">if</span> <span class="k">case</span> <span class="k">let</span> <span class="nv">x</span><span class="p">?</span> <span class="o">=</span> <span class="n">x</span> <span class="p">{</span>
<span class="nf">printSomething</span><span class="p">()</span>
<span class="p">}</span>
<span class="k">if</span> <span class="k">case</span> <span class="o">.</span><span class="kt">Some</span><span class="p">(</span><span class="k">let</span> <span class="nv">x</span><span class="p">)</span> <span class="o">=</span> <span class="n">x</span> <span class="p">{</span>
<span class="nf">printSomething</span><span class="p">()</span>
<span class="p">}</span>
</code></pre></div></description>
<pubDate>Mon, 09 May 2016 08:00:00 -0400</pubDate>
<link>http://iosdev.nyc/blog/if-let-where/</link>
<guid isPermaLink="true">http://iosdev.nyc/blog/if-let-where/</guid>
</item>
<item>
<title>Let and guard statements in Swift</title>
<description><p><strong>Update, 07 Feb 2017</strong><br>
As of Swift 3, some of the code below may not work properly anymore. To see what changed, read <a href="/blog/let-guard-pyramid-of-doom-swift-3/">Pyramid of Doom Updated (Swift 3)</a>.</p>
<hr>
<p><strong>Original Post</strong></p>
<p>Swift 2 was announced in June, soon to be a year ago. Still, some of the concepts it introduced are new to many iOS developers – especially the ones who keep using ObjC as their #1 language and are only starting to learn Swift.</p>
<p>One of the new things introduced in Swift 2 is guard statement, which I think finally solves Swift’s pyramid of doom (or what I called <em>“let indentation hell”</em> – that’s what you get when you are nesting <a href="https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html">optional binding</a> statements one inside another).</p>
<p>First, let’s take a look at hypothetical code of setting an author on a book, in Swift &lt; 1.2</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">letIndentationHell</span><span class="p">(</span><span class="nv">foo</span><span class="p">:</span> <span class="kt">Book</span><span class="p">?,</span> <span class="nv">bar</span><span class="p">:</span> <span class="kt">Author</span><span class="p">?)</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">book</span> <span class="o">=</span> <span class="n">foo</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">author</span> <span class="o">=</span> <span class="n">bar</span> <span class="p">{</span>
<span class="n">book</span><span class="o">.</span><span class="nf">setAuthor</span><span class="p">(</span><span class="n">author</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>That was one of the things that really terrified me when I first started using Swift 1.0 – and when you were also using ObjC-bridged code, this nesting could have been even deeper.</p>
<p>Swift 1.2 allowed us to bind multiple optionals in one statement</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">multipleBind</span><span class="p">(</span><span class="nv">foo</span><span class="p">:</span> <span class="kt">Book</span><span class="p">?,</span> <span class="nv">bar</span><span class="p">:</span> <span class="kt">Author</span><span class="p">?)</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">book</span> <span class="o">=</span> <span class="n">foo</span><span class="p">,</span> <span class="k">let</span> <span class="nv">author</span> <span class="o">=</span> <span class="n">bar</span> <span class="p">{</span>
<span class="n">book</span><span class="o">.</span><span class="nf">setAuthor</span><span class="p">(</span><span class="n">author</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>That already looks better, but still, it forces you to move most of your function’s body inside indented blocks.</p>
<p>You could avoid it, but it would require ObjC-style <code>nil</code> checking, and while it works, I just personally don’t like it as much – Swift was supposed to be better than ObjC, right?</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">objcStyle</span><span class="p">(</span><span class="nv">foo</span><span class="p">:</span> <span class="kt">Book</span><span class="p">?,</span> <span class="nv">bar</span><span class="p">:</span> <span class="kt">Author</span><span class="p">?)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">foo</span> <span class="o">==</span> <span class="kc">nil</span> <span class="o">&amp;&amp;</span> <span class="n">bar</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="k">let</span> <span class="nv">book</span> <span class="o">=</span> <span class="n">foo</span><span class="o">!</span>
<span class="k">let</span> <span class="nv">author</span> <span class="o">=</span> <span class="n">bar</span><span class="o">!</span>
<span class="n">book</span><span class="o">.</span><span class="nf">setAuthor</span><span class="p">(</span><span class="n">author</span><span class="p">)</span>
<span class="c1">// or force unwrap both directly with</span>
<span class="c1">// foo!.setAuthor(bar!)</span>
<span class="p">}</span>
</code></pre></div>
<p>Function above is longer than it could be and it’s also really easy to write wrong condition for the if statement – actually that’s what I did writing it for the first time for this post.</p>
<p>If you try the code above and one of the <code>obj1</code> or <code>obj2</code> is equal to <code>nil</code> – you will get an error.<br>
(proper condition is <code>if (obj1 == nil || obj2 == nil)</code>)</p>
<p>What Swift 2 introduced is actually a combination of solutions suggested in example #2 and #3 – it’s called guard statement.</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">swiftGuardStyle</span><span class="p">(</span><span class="nv">foo</span><span class="p">:</span> <span class="kt">Book</span><span class="p">?,</span> <span class="nv">bar</span><span class="p">:</span> <span class="kt">Author</span><span class="p">?)</span> <span class="p">{</span>
<span class="k">guard</span> <span class="k">let</span> <span class="nv">book</span> <span class="o">=</span> <span class="n">foo</span><span class="p">,</span> <span class="n">author</span> <span class="o">=</span> <span class="n">bar</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="n">book</span><span class="o">.</span><span class="nf">setAuthor</span><span class="p">(</span><span class="n">author</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div>
<p>Now, all the optionals can be checked at the beginning of your function and you can provide a code, that will be executed if the condition fails (inside the <code>else</code> block).</p>
<p>Actually if you forget to provide the code for when condition fails, compiler will throw an error! <code>Guard</code> must always have an <code>else</code> clause, that needs to contain <code>return</code> or <code>break</code> inside.</p>
<p>This also allows you to name objects passed to your functions and to your closures in a sane way.</p>
<p>I used to name function arguments and local variables differently – so I could clearly see which variables are of optional type and which ones are already unwrapped.</p>
<p>In this case, instead of taking variables named <code>foo</code> and <code>bar</code> as function parameters, you could rewrite your code with <code>book</code> and <code>author</code></p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">swiftGuardStyle2</span><span class="p">(</span><span class="nv">book</span><span class="p">:</span> <span class="kt">Book</span><span class="p">?,</span> <span class="nv">author</span><span class="p">:</span> <span class="kt">Author</span><span class="p">?)</span> <span class="p">{</span>
<span class="k">guard</span> <span class="k">let</span> <span class="nv">book</span> <span class="o">=</span> <span class="n">book</span><span class="p">,</span> <span class="n">author</span> <span class="o">=</span> <span class="n">author</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="n">book</span><span class="o">.</span><span class="nf">setAuthor</span><span class="p">(</span><span class="n">author</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div>
<p>(this could have also been done in previous examples, but it feels much cleaner with <code>guard</code>)</p>
<p>You can also combine <code>guard</code> with <code>where</code>, to check specific condition on your optionals AND – you can use it to check conditions on non-optional values!<br>
(I found that not long ago on <strong>Eric Cerney</strong>&rsquo;s <a href="http://ericcerney.com/swift-guard-statement/">post</a>)</p>
<div class="highlight"><pre><code class="language-swift" data-lang="swift"><span class="kd">func</span> <span class="nf">swiftGuardStyleWhere</span><span class="p">(</span><span class="nv">book</span><span class="p">:</span> <span class="kt">Book</span><span class="p">?,</span> <span class="nv">author</span><span class="p">:</span> <span class="kt">Author</span><span class="p">?,</span> <span class="nv">rating</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="p">{</span>
<span class="k">guard</span> <span class="n">rating</span> <span class="o">&gt;</span> <span class="mi">4</span><span class="p">,</span> <span class="k">let</span> <span class="nv">book</span> <span class="o">=</span> <span class="n">book</span><span class="p">,</span> <span class="n">author</span> <span class="o">=</span> <span class="n">author</span> <span class="k">where</span> <span class="n">author</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s">"Bob"</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="c1">// will only execute if book and author are non nil</span>
<span class="c1">// and when author's name is "Bob"</span>
<span class="c1">// and when rating is &gt; 4</span>
<span class="n">book</span><span class="o">.</span><span class="nf">setAuthor</span><span class="p">(</span><span class="n">author</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div>
<p>With <code>guard</code>, Swift programming becomes easier and your code becomes cleaner – you can now check for expected conditions, not for error case (like with <code>foo == nil</code>) and as a bonus – you’re getting rid of that ugly indentation.</p>
<p>You can find all code examples from the post on <a href="https://gist.github.com/MariuszWisniewski/ef5744b6d39b87e0f32f">GitHub</a>.</p>
</description>
<pubDate>Sun, 24 Apr 2016 08:00:00 -0400</pubDate>
<link>http://iosdev.nyc/blog/let-guard/</link>
<guid isPermaLink="true">http://iosdev.nyc/blog/let-guard/</guid>
</item>
</channel>
</rss>