尝试使用一些管道改变函数中的字符串将返回空数组

时间:2016-04-21 07:13:51

标签: elixir

对不起这个可怕的标题,这就是问题所在。

我正在书中练习我想要学习的内容:

从很多单词的列表中,只找到以" D"开头的单词,大写单词,并按最小到最大排序。

这是我到目前为止所做的:

defmodule ReadFile do
    def findD(contents) do
        contents
        |> String.split("\n")
        |> Enum.filter(fn(word) -> String.starts_with?(word, "D") end)
        |> Enum.map(fn(word) -> String.upcase(word) end)
        |> Enum.sort(fn(el1, el2) -> String.length(el1) > String.length(el2) end)
    end
end

我传递给findD的数据是

(只是从file.txt`

中读取的一串字
"adult\n\naeroplane\n\nair\n\naircraft Carrier\n\nairforce\n\nairport\n\nalbum\n\nalphabet\n\napple\n\narm\n\narmy\n\nbaby\n\nbaby\n\nbackpack\n\nballoon\n\nbanana\n\nbank\n\nbarbecue\n\nbathroom\n\nbathtub\n\nbed\n\nbed\n\nbee\n\nbible\n\nbible\n\nbird\n\nbomb\n\nbook\n\nboss\n\nbottle\n\nbowl\n\nbox\n\nboy\n\nbrain\n\nbridge\n\nbutterfly\n\nbutton\n\ncappuccino\n\ncar\n\ncar-race\n\ncarpet\n\ncarrot\n\ncave\n\nchair\n\nchess Board\n\nchief\n\nchild\n\nchisel\n\nchocolates\n\nchurch\n\nchurch\n\ncircle\n\ncircus\n\ncircus\n\nclock\n\nclown\n\ncoffee\n\ncoffee-shop\n\ncomet\n\ncompact Disc\n\ncompass\n\ncomputer\n\ncrystal\n\ncup\n\ncycle\n\ndata Base\n\ndesk\n\ndiamond\n\ndress\n\ndrill\n\ndrink\n\ndrum\n\ndung\n\nears\n\nearth\n\negg\n\nelectricity\n\nelephant\n\neraser\n\nexplosive\n\neyes\n\nfamily\n\nfan\n\nfeather\n\nfestival\n\nfilm\n\nfinger\n\nfire\n\nfloodlight\n\nflower\n\nfoot\n\nfork\n\nfreeway\n\nfruit\n\nfungus\n\ngame\n\ngarden\n\ngas\n\ngate\n\ngemstone\n\ngirl\n\ngloves\n\ngod\n\ngrapes\n\nguitar\n\nhammer\n\nhat\n\nhieroglyph\n\nhighway\n\nhoroscope\n\nhorse\n\nhose\n\nice\n\nice-cream\n\ninsect\n\njet fighter\n\njunk\n\nkaleidoscope\n\nkitchen\n\nknife\n\nleather jacket\n\nleg\n\nlibrary\n\nliquid\n\nmagnet\n\nman\n\nmap\n\nmaze\n\nmeat\n\nmeteor\n\nmicroscope\n\nmilk\n\nmilkshake\n\nmist\n\nmoney $$$$\n\nmonster\n\nmosquito\n\nmouth\n\nnail\n\nnavy\n\nnecklace\n\nneedle\n\nonion\n\npaintBrush\n\npants\n\nparachute\n\npassport\n\npebble\n\npendulum\n\npepper\n\nperfume\n\npillow\n\nplane\n\nplanet\n\npocket\n\npost-office\n\npotato\n\nprinter\n\nprison\n\npyramid\n\nradar\n\nrainbow\n\nrecord\n\nrestaurant\n\nrifle\n\nring\n\nrobot\n\nrock\n\nrocket\n\nroof\n\nroom\n\nrope\n\nsaddle\n\nsalt\n\nsandpaper\n\nsandwich\n\nsatellite\n\nschool\n\nsex\n\nship\n\nshoes\n\nshop\n\nshower\n\nsignature\n\nskeleton\n\nslave\n\nsnail\n\nsoftware\n\nsolid\n\nspace Shuttle\n\nspectrum\n\nsphere\n\nspice\n\nspiral\n\nspoon\n\nsports-car\n\nspot Light\n\nsquare\n\nstaircase\n\nstar\n\nstomach\n\nsun\n\nsunglasses\n\nsurveyor\n\nswimming Pool\n\nsword\n\ntable\n\ntapestry\n\nteeth\n\ntelescope\n\ntelevision\n\ntennis racquet\n\nthermometer\n\ntiger\n\ntoilet\n\ntongue\n\ntorch\n\ntorpedo\n\ntrain\n\ntreadmill\n\ntriangle\n\ntunnel\n\ntypewriter\n\numbrella\n\nvacuum\n\nvampire\n\nvideotape\n\nvulture\n\nwater\n\nweapon\n\nweb\n\nwheelchair\n\nwindow\n\nwoman\n\nworm\n\nx-ray\n\n"

iex我正在做

{ :ok, fileContents } = File.read("sample.txt")

ReadFile.findD(fileContents)返回[]

我不确定为什么,我也不知道如何调试pipeline我试过插入

|> IO.puts

在管道中的每次调用之后,但这只会导致错误。任何帮助都会非常感谢。

1 个答案:

答案 0 :(得分:1)

如果您提供的单词列表是您实际使用的单词列表,那么问题是没有单词以"D"开头,因此[]是正确的结果。

如果您想要以小写字母"d"开头的单词,则必须替换此行:

Enum.filter(fn(word) -> String.starts_with?(word, "D") end)

使用:

Enum.filter(fn(word) -> String.starts_with?(word, "d") end)

如果你想检查两者,你可以这样做:

Enum.filter(fn(word) -> String.at(word, 0) in ["d", "D"] end)