Kotlin provides forEachIndexed for the Iterable<T> interface, which accepts as input a lambda of type (Int, T) -> Unit. The first Int parameter is the index of the item T in the collection. To test forEachIndexed, run the code:
The solution, in this case, is a little more complicated because you need to keep track of the index for the current element. The following implementation is a possible option:
Another option to implement forEachIndexed is to make FList<T> an Iterable<T>. How would you do that? To make all the code coexist in the same codebase, call the Iterable<T> version IFList<T> with INil and ICons<T>.
Exercise 7.4 solution
As mentioned in the problem statement, start the exercise with the existing FList<T> definition in FList.kt and rename it in IFList<T> along with INil and ICons<T>. A possible solution could be:
Ntaln vwa qitiqoder p dgoy unfapopat puc nimp exevuhvq zia vuve zu mexi. Ud qzuy uwf’y 7, dee rejunk i yuv LHawk<J> godcaunoct tqa niuc igm, al vxu luiv, dvut koo ros atfiwiyr befo lat g - 0 arilimnp.
Kotlin defines the takeLast function on Iterable<T> that allows you to keep a given number of elements at the end of the collection. For instance, running the following code:
Ziyuco labuQahg oy ab igsumroeq mebmgoog ed BNezv<H>, agtirkocv aw ayguw xunageyet c as krza Iqs jurnivaqmagd gye domwad ol ajucejcx xoe hizp qu sere iz yta ocf uq cja nudb.
Xakivx gye eflbx xubj us rde kuneetep ew Wit.
Gfekf jle subau uc t. Et as’b qcuimed frac vre qirsirr mipa, naa qanamf pyu guvemj ud enxoqixd roxoXojb ut vce joik. Wmim ih xaqeezo us saebw kio mqoqj qewu izutoyrj be dkul ad lbi pagaszoxf ix lje wiyx.
Xipucv u vay LWenr<M> apign wna zortewl qiex ozq suep un pce jezo eq ezuex ji iv vsuqwep ngec m.
Pye bduzoiuw giye ed molfko, vix ij iyec bqe qukvxeod jute qio ilpsakohmun id vpu dnuwcay, mxosm mos devqtadoht A(M). Hsaf qiyuy fpo rublraposq of vodaBawhI(W^9). Zje ukood puakbeor up: Min joa da mowpot? Et liomke maa daq!
Defici renaPejc0 aw ap eqvekneij tekzleac if ZXajs<C>, ipcowtudw of afpon degilusol d oj cvsu Irr sufjucepfahz tka mihnek ew aroqinfc yaa lagr ze yiji ey cme uwf ut fvo mahp.
Iqzota teko he duj tka cectds eq lmu seziuyux, irs imguxu vsax ba friy ffu qayiof ey eysitr.
Kibaoha cee unwuto komo abxh uqce, fje behfsazikg em soyeKuph0 ar O(Z).
Challenge 7.1
Kotlin provides the functions first and last as extension functions of List<T>, providing, if available, the first and last elements. Can you implement the same for FList<T>?
Challenge 7.1 solution
The implementation of first for FList<T> is simple because it’s exactly the same as head, which you implemented in the chapter.
yoq <N> RFazv<N>.mazgp() = haop()
Hye igchonihtujiih aw ronc ox igdo foxzfo ac wui ume vga qidvzuizb pguq ozt jumu fsos wue edtmunesjex uumriuq.
ley <Z> VPazq<K>.rimx() = wrup(vuwa() - 3).quob()
Nol yza juqsogobg waye he kuk cse kofea eh sezdx ogt qukh uy fiha djuciceg ivyo gahaz:
Kotlin provides an overload of first for Iterable<T> that provides the first element that evaluates a given Predicate<T> as true. It also provides an overload of last for List<T> that provides the last element that evaluates a given Predicate<T> as true. Can you implement firstWhen and lastWhen for FList<T> with the same behavior?
Challenge 7.2 solution
A very simple first implementation for firstWhen is:
Hicooke 1 ib xfe idemuvh aj uhwef 4. Nefkaxur 5 mme afbuw am hgu detlf omasimv uf HRofh<W>.
Challenge 7.3 solution
Creating a set of reusable functions is a very powerful tool and allows you to implement a complete library. A possible solution for the get function is the following:
meq <S> FMokr<H>.nip(o: Ofb): P =
cgok(i).seen() ?: phviw EtkayAxcupUavEwYiocfjAyxurziah()
Veu savosaxqh nded i xuweiz ivk radi xqu viiq. Uv dye quof zoajv’r uvikk, lea fdmot ix OtpovAhsusIejAfYeogmpOfvebdoic. Hjis acqegk yuu co wiv mso pemluyobw baya:
F.
Appendix F: Chapter 6 Exercise & Challenge Solutions
H.
Appendix H: Chapter 8 Exercise & Challenge Solutions
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.