1 00:00:00,640 --> 00:00:03,600 In this session, we'll talk about loops in Pharo. 2 00:00:03,760 --> 00:00:08,320 In Pharo, loops are messages sent to objects. 3 00:00:08,480 --> 00:00:10,680 There are many types of loops. 4 00:00:11,320 --> 00:00:14,680 They're messages sent to numbers, collections, 5 00:00:14,840 --> 00:00:16,360 or even to blocks. 6 00:00:16,520 --> 00:00:21,040 In some cases, they are iterators over the elements of a collection. 7 00:00:22,080 --> 00:00:23,600 This is an example: 8 00:00:23,760 --> 00:00:27,240 I send the message timesRepeat: 9 00:00:27,400 --> 00:00:29,000 to the integer 4. 10 00:00:29,160 --> 00:00:31,640 I pass it a block as a parameter. 11 00:00:31,800 --> 00:00:34,480 We studied blocks in the previous course. 12 00:00:34,640 --> 00:00:38,400 This blocks starts here and ends there. 13 00:00:39,080 --> 00:00:43,520 This message repeats the evaluation of this block 14 00:00:44,120 --> 00:00:46,960 several times: in this example, four times 15 00:00:47,120 --> 00:00:48,840 as the receiver is 4. 16 00:00:50,720 --> 00:00:54,080 There are other types of messages to create loops. 17 00:00:54,240 --> 00:00:57,000 In this example, it's the message to:do:. 18 00:00:57,160 --> 00:01:00,360 The message to:do: is defined on the class Number. 19 00:01:01,520 --> 00:01:04,360 I'll also pass it a block as a parameter, 20 00:01:04,520 --> 00:01:06,400 the last argument of the message. 21 00:01:06,560 --> 00:01:09,440 This block gets executed a number of times 22 00:01:09,600 --> 00:01:13,640 with a loop cursor that goes from the receiver 23 00:01:13,800 --> 00:01:16,320 to the first argument of the message. 24 00:01:17,800 --> 00:01:19,960 For instance, 25 00:01:20,120 --> 00:01:23,080 with the exact same example: 26 00:01:23,240 --> 00:01:26,360 I send the message to:do: to the integer 1. 27 00:01:27,160 --> 00:01:31,840 In the block, you display the parameter of the block, 28 00:01:32,000 --> 00:01:34,080 the parameter i at each loop. 29 00:01:34,240 --> 00:01:39,200 In the transcript, you have all the integers between 1 and 100. 30 00:01:42,000 --> 00:01:47,120 There are other messages to create loops. 31 00:01:47,280 --> 00:01:50,280 For instance, the message to:by:do 32 00:01:50,440 --> 00:01:52,440 to increment by more than 1. 33 00:01:52,600 --> 00:01:55,680 You control the scale of the increment. 34 00:01:55,840 --> 00:01:58,040 You can increment by 3 in this example. 35 00:01:58,200 --> 00:02:02,160 With every loop, i takes values by step of 3. 36 00:02:03,920 --> 00:02:08,080 You can see what happens when you execute this bit of code. 37 00:02:08,240 --> 00:02:12,040 You can see i went from 1 to 100 38 00:02:12,200 --> 00:02:14,400 with an increment of 3 each time. 39 00:02:15,880 --> 00:02:18,920 There are also iterators to create loops. 40 00:02:19,080 --> 00:02:23,360 Iterators are messages that must be sent to collections, 41 00:02:23,520 --> 00:02:25,000 to collection objects. 42 00:02:25,160 --> 00:02:28,440 There are many types such as do:. 43 00:02:28,600 --> 00:02:31,880 do: iterates over every element of a collection. 44 00:02:32,040 --> 00:02:35,400 collect: iterates over the elements of a collection 45 00:02:35,560 --> 00:02:39,040 but also collects the results 46 00:02:39,200 --> 00:02:41,040 to put them in a new collection. 47 00:02:41,200 --> 00:02:45,560 select: selects matching elements in a collection: 48 00:02:45,720 --> 00:02:48,720 you build a new collection with those you selected. 49 00:02:48,880 --> 00:02:53,280 reject: rejects the elements of a collection you don't want. 50 00:02:53,440 --> 00:02:56,320 detect: helps to determine 51 00:02:56,480 --> 00:03:00,120 whether an element exists in a given collection, etc. 52 00:03:00,280 --> 00:03:03,360 We'll study some of these in the next slides. 53 00:03:04,200 --> 00:03:08,440 do:, for instance, is the most common message. 54 00:03:08,600 --> 00:03:13,000 You send the message do: to a collection and pass it a block. 55 00:03:13,160 --> 00:03:16,240 With every loop, 56 00:03:16,400 --> 00:03:19,160 the parameter of the block, :each, 57 00:03:19,320 --> 00:03:22,200 receives the first element of the collection, 58 00:03:22,360 --> 00:03:26,000 then the second, until the final element of the collection. 59 00:03:28,280 --> 00:03:31,680 This is an example: it's a defined collection. 60 00:03:31,840 --> 00:03:34,200 A reminder of the syntax: open with #( 61 00:03:34,360 --> 00:03:37,080 and close with ) for literal collections. 62 00:03:37,240 --> 00:03:42,120 This collection contains four integers: 15, 10, 19, and 68. 63 00:03:42,280 --> 00:03:46,320 You send the message do: to the collection and pass it a block. 64 00:03:46,480 --> 00:03:51,000 i, the parameter of the block, 65 00:03:51,160 --> 00:03:53,560 takes the value 15 on the first loop, 66 00:03:53,720 --> 00:03:56,120 then 10, 19, and 68. 67 00:03:59,040 --> 00:04:01,680 This is another message: whileTrue:. 68 00:04:01,840 --> 00:04:04,680 whileTrue: can also create loops, 69 00:04:04,840 --> 00:04:07,640 but it is a message you send to a block. 70 00:04:07,800 --> 00:04:09,800 It is defined over the class Block. 71 00:04:09,960 --> 00:04:12,480 This is the receiver block. 72 00:04:12,640 --> 00:04:14,720 There's a block of arguments here. 73 00:04:15,320 --> 00:04:19,320 I give you a concrete example from the class Color. 74 00:04:19,480 --> 00:04:22,120 This is whileTrue:. 75 00:04:22,760 --> 00:04:25,800 In the receiver block, there's a condition. 76 00:04:25,960 --> 00:04:28,560 The block is evaluated to true or false. 77 00:04:28,720 --> 00:04:32,200 Depending on the evaluation of the first block, the receiver, 78 00:04:32,360 --> 00:04:34,800 you trigger or don't trigger the execution 79 00:04:34,960 --> 00:04:38,600 of the second block, which is the parameter of whileTrue:. 80 00:04:39,520 --> 00:04:42,080 There's another version of whileTrue:. 81 00:04:42,240 --> 00:04:46,480 It's the method whileTrue without a parameter. 82 00:04:46,640 --> 00:04:50,280 It takes the receiver block and evaluates it. 83 00:04:50,440 --> 00:04:52,920 If it returns true or false, 84 00:04:53,080 --> 00:04:55,800 it is re-evaluated once more, and so on. 85 00:04:55,960 --> 00:04:59,240 You re-evaluate the block as long as its value is true. 86 00:05:00,840 --> 00:05:04,960 You have the twins of whileTrue with the methods whileFalse, 87 00:05:05,120 --> 00:05:07,440 with and without parameters. 88 00:05:07,600 --> 00:05:12,120 Every variation exists when it comes to classes and blocks. 89 00:05:14,160 --> 00:05:16,640 To sum up, in this course, 90 00:05:16,800 --> 00:05:20,840 we saw that loops are expressed as normal messages in Pharo. 91 00:05:21,000 --> 00:05:22,440 They're sent to objects 92 00:05:22,600 --> 00:05:27,560 (integers, collections, blocks...). 93 00:05:28,280 --> 00:05:32,400 You can use them to build plenty of different loops. 94 00:05:32,560 --> 00:05:34,680 You can even add some if you wish.