Skip to Content
深入理解運算原理|從簡單的機器到無所不能的程式
book

深入理解運算原理|從簡單的機器到無所不能的程式

by Tom Stuart
November 2017
Beginner to intermediate
344 pages
7h 2m
Chinese
GoTop Information, Inc.
Content preview from 深入理解運算原理|從簡單的機器到無所不能的程式
無為的程式設計
|
175
(ONE..HUNDRED).map do |n|
if (n % FIFTEEN).zero?
'FizzBuzz'
elsif (n % THREE).zero?
'Fizz'
elsif (n % FIVE).zero?
'Buzz'
else
n.to_s
end
end
一如先前的承諾,為了讓程式碼更清楚,我們正在編寫
ONE
(而非
-> p {
-> x {p [x]}}
及其他)。
不幸的是,這個程式再也無法運作了,因為我們現在用在以 proc 為基礎的數值實作物上的
是像
..
%
的運算。因為 Ruby 不知道如何將它們視為數值,結果就只是掛點:
TypeError:
can't iterate from Proc
(譯註:型別錯誤,因為 Proc 無法反覆)、
NoMethodError:
undefined method '%' for #<Proc (lambda)>
(譯註:方法錯誤,因為未定義)…等。我們
需要以這些表示方式換掉所有的運算才能正常運作,而且只能使用 proc 來完成。
在我們可以重新實作任何運算之前,我們需要
true
false
的實作物。
布林值
只使用 proc 要如何表示布林值?我們會在條件陳述式使用單獨存在的布林值,而條件
通常是指『如果有一些布林值,就這麼做,不然就那樣做』:
>> success = true
=> true
>> if success then 'happy' else 'sad' end
=> "happy"
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JSON實務手冊

JSON實務手冊

Tom Marrs

Publisher Resources

ISBN: 9789864766000