Posts Tagged ‘ Coding

[Kotlin]研究 Kotlin 的函式宣告、函式型別、匿名函式、Lambda 與高階函式

Kotlin 的「函式」是所謂的「一級函式」,支援「高階函式」的用法,也可宣告「匿名函式」及「巢狀函式」,這些都是近幾年所興起的程式語言特色。

(原本這篇文章只是要整理一下 Kotlin 的函式用法而已,沒想到愈寫愈多!!)

名詞定義

先確認是否了解什麼是表達式? 什麼是敘述式?

Expression (表達式、表示式、運算式)

  • 它是一種「值」
  • 會傳回結果
  • 單獨存在沒有意義
  • 可放在「等號」的右邊
  • 可做為函式的引數 (Argument)
  • 可做為函式的傳回值
  • 例如: 數值、字串、布林值、null、運算後的結果、比較後的結果、匿名函式...

Statement (陳述式、敘述式、語句)

  • 由會產生「動作」的程式關鍵字及語法所組成的程式碼
  • 不會傳回結果
  • 例如: 流程控制、迴圈、宣告、函式、類別...

其它名詞定義

  • Literal: 字面值,例如: 103.14truenull'A'"This is a book"...
  • Parameter: 參數,函式「宣告」時所輸入的值,例如: fun example(參數) { }
  • Argument: 引數,函式「執行」時所引用的值,例如: example(引數)
  • Identifier: 識別字,命名變數、函式、類別...時所使用的文字
  • Lambda: 一種匿名函式的寫法或概念

Read more

[Perl]Windows 上功能最齊全的 Perl 整合開發環境: DWIM Perl

功欲善其事,必先利其器!!

如果你在 Windows 上找不到容易上手的 Perl 整合開發環境(IDE),那你可以試試「DWIM Perl」。

「DWIM Perl」主要是由「Padre」、「StrawberryPerl」及一些常用的 CPAN 模組所組成,「DWIM」是「Do What I Mean」的縮寫,因此可以看得到它的用心,其中:

  • Padre」是一套 Perl 的程式編輯器,代表的圖像就是右邊那隻美麗的蝴蝶。
  • StrawberryPerl」與「ActivePerl」一樣,是一套運作在 Windows 上的 Perl 直譯器,但 StrawberryPerl 是 OpenSource,並且本身就已經集成了很多常用的 CPAN 模組,StrawberryPerl 的代表圖像就是一顆嬌豔欲滴、但有點曝光過度的大草莓

檔案下載: DWIM Perl

安裝「DWIM Perl」時請安裝在你的 D 糟,安裝好之後只要執行「Padre, the Perl IDE」就會進入它的整合開發環境。

Read more

[轉載]21 天教你學會 C++

轉載自: 酷壳 - 21 天教你学会 C++


下面是一個《Teach Yourself C++ in 21 Days》的流程圖, 請各位程式設計師同仁認真領會。如果有必要, 你可以查看這個圖書以作參照: http: //www.china-pub.com/27043

看完上面這個圖片, 我在想, 我學習 C++ 有 12 年了, 好像 C++ 也沒有學得特別懂, 看到 STL 和泛型, 還是很頭大。不過, 我應該去考慮研究量子物理和生物化學, 這樣, 我才能重返 98 年殺掉還在大學的我, 然後達到 21 天搞定 C++ 的目標。另外, 得要特別提醒剛剛開始學習 C++ 的朋友, 第 21 天的時候, 小心被人殺害。呵呵。

Read more

[jQuery]《網頁設計-愛上jQuery》讀書心得及評論

使用 JavaScrip 的程式庫來開發網頁的程式可以加快開發的速度,其中 jQuery 也是頗受歡迎的一員。

不過目前可以買得到的 jQuery 中文書籍實在寥寥可數,以下是最近閱讀《網頁設計-愛上jQuery》這本書的心得及評論 (作者是羅子洋)。 Read more

[轉載][MySQL]MySQL 的 SET NAMES xxx 字元編碼問題分析

轉載自: PHPChina


近來接受 BBT 的培訓,做一個投票系統。系統程式碼倒不是很難,但是我的時間主要花費在了研究字符集和編碼上面。MySQL 和 Apache 兩個系統的編碼 (字符集) 問題讓我費勁腦筋,吃盡苦頭。網上對這些問題的解決比較零散,比較片面,大部分是提供解決方法,卻不說為什麼。於是我將這幾天收穫總結一下,避免後來者再走彎路。這篇文章對 PHP 編寫有一點幫助 (看完你就知道,怎樣讓你的 PHP 程式在大部分空間提供商的伺服器裡顯示正常),但是更多幫助在於網路伺服器的架設和設置。
Read more

[JavaScript]動態網頁設計的相關網頁 (dhtml)

國外網站

中文網站

其它

[Perl][JavaScript]以數學的原理處理四捨五入

我看過太多人都把四捨五入的問題看成是「字串」來處理,實際上,如果把四捨五入做為「數學」來解的話,程式大概只要幾行即可解決。

以四捨五入取整數的例子來講,其原理就是: 任何數值 +0.5 再取整數

但要注意的是,當四捨五入遇到「負數」時就會變成「五捨六入」,這是因為中間數要往數值大的那邊進位的緣故。所以加上了負數的處理,程式至少又多了一行:
Read more

在 Blogger 中使用 dp.SyntaxHighlighter 顯示程式碼

db.SyntaxHighlighter 是一支不錯的程式碼高亮度顯示程式,特點為:

  1. 顯示行號
  2. 支援12種程式語言
  3. 模組化載入需要的程式語言
  4. 支援 “<pre>” 及 “<textarea>” 區塊
  5. 純文字檢視
  6. 複製到剪貼簿
  7. 直接列印程式碼

缺點就是載入速度較慢。

安裝步驟

  1. 修改 blogger 樣板,搜尋 “</body>”,在 “上方” 加入(你可以選擇你要的語言載入即可):
    <script class='javascript' src='http://syntaxhighlighter.googlecode.com/svn/tags/1.5.0/Scripts/shCore.js'></script>
    <script class='javascript' src='http://syntaxhighlighter.googlecode.com/svn/tags/1.5.0/Scripts/shBrushPhp.js'></script>
    <script class='javascript' src='http://syntaxhighlighter.googlecode.com/svn/tags/1.5.0/Scripts/shBrushJScript.js'></script>
    <script class='javascript' src='http://syntaxhighlighter.googlecode.com/svn/tags/1.5.0/Scripts/shBrushSql.js'></script>
    <script class='javascript' src='http://syntaxhighlighter.googlecode.com/svn/tags/1.5.0/Scripts/shBrushXml.js'></script>
    <script class='javascript' src='http://syntaxhighlighter.googlecode.com/svn/tags/1.5.0/Scripts/shBrushDelphi.js'></script>
    <script class='javascript' src='http://syntaxhighlighter.googlecode.com/svn/tags/1.5.0/Scripts/shBrushPython.js'></script>
    <script class='javascript' src='http://syntaxhighlighter.googlecode.com/svn/tags/1.5.0/Scripts/shBrushRuby.js'></script>
    <script class='javascript' src='http://syntaxhighlighter.googlecode.com/svn/tags/1.5.0/Scripts/shBrushCss.js'></script>
    <script class='javascript' src='http://syntaxhighlighter.googlecode.com/svn/tags/1.5.0/Scripts/shBrushCpp.js'></script>
    <script class='javascript'>
    dp.SyntaxHighlighter.ClipboardSwf = 'http://syntaxhighlighter.googlecode.com/svn/tags/1.5.0/Scripts/clipboard.swf';
    dp.SyntaxHighlighter.HighlightAll('code');
    </script>

    (因為 “偷吃步” 的關係,程式直接連到 dp.SyntaxHighlighter 的 SVN 上,你可以視情況下載回來放到自己的伺服器)

  2. dp.SyntaxHighlighter 原本已經有一個 CSS 檔了,但如果將這個 CSS 檔以 “Link” 的方式載入,卻無法正常顯示。所以,請自行增加下列 CSS 到 blogger 樣板的 CSS 區段:
    /*dp.highlighter*/
    .dp-highlighter {
      font-family: "Consolas", "Courier New", Courier, mono;
      font-size: 12px;
      background-color: #E7E5DC;
      width: 99%;
      overflow: auto;
      margin: 18px 0px 18px 0px;
      padding-top: 1px; /* adds a little border on top when controls are hidden */
      border:1px inset;
      max-height:200px;
    }
    .dp-highlighter .bar {
      padding-left: 45px;
    }
    .dp-highlighter.collapsed .bar,
    .dp-highlighter.nogutter .bar {
      padding-left: 0px;
    }
    .dp-highlighter ol {
      list-style: decimal; /* for ie */
      list-style: decimal-leading-zero; /* better look for others */
      background-color: #fff;
      margin: 0px 0px 1px 45px; /* 1px bottom margin seems to fix occasional Firefox scrolling */
      padding: 0px;
      color: #5C5C5C;
    }
    .dp-highlighter.nogutter ol {
      list-style-type: none !important;
      margin-left: 0px;
    }
    .dp-highlighter ol li,
    .dp-highlighter .columns div {
      border-left: 3px solid #6CE26C;
      background-color: #f8f8f8;
      padding-left: 10px;
      line-height: 14px;
    }
    .dp-highlighter.nogutter ol li,
    .dp-highlighter.nogutter .columns div {
      border: 0;
    }
    .dp-highlighter .columns {
      color: gray;
      overflow: hidden;
      width: 100%;
    }
    .dp-highlighter .columns div {
      padding-bottom: 5px;
    }
    .dp-highlighter ol li.alt {
      background-color: #fff;
    }
    .dp-highlighter ol li span {
      color: Black;
    }
    /* Adjust some properties when collapsed */
    .dp-highlighter.collapsed ol {
      margin: 0px;
    }
    .dp-highlighter.collapsed ol li {
      display: none;
    }
    /* Additional modifications when in print-view */
    .dp-highlighter.printing {
      border: none;
    }
    .dp-highlighter.printing .tools {
      display: none !important;
    }
    .dp-highlighter.printing li {
      display: list-item !important;
    }
    /* Styles for the tools */
    .dp-highlighter .tools {
      padding: 3px 8px 3px 10px;
      font: 9px Verdana, Geneva, Arial, Helvetica, sans-serif;
      color: silver;
      background-color: #f8f8f8;
      text-align1: right;
      padding-bottom: 10px;
      border-left: 3px solid #6CE26C;
    }
    .dp-highlighter.nogutter .tools {
      border-left: 0;
    }
    .dp-highlighter.collapsed .tools {
      border-bottom: 0;
    }
    .dp-highlighter .tools a {
      font-size: 9px;
      color: #a0a0a0;
      text-decoration: none;
      margin-right: 10px;
    }
    .dp-highlighter .tools a:hover {
      color: red;
      text-decoration: underline;
    }
    /* About dialog styles */
    .dp-about { background-color: #fff; margin: 0px; padding: 0px; }
    .dp-about table { width: 100%; height: 100%; font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; }
    .dp-about td { padding: 10px; vertical-align: top; }
    .dp-about .copy { border-bottom: 1px solid #ACA899; height: 95%; }
    .dp-about .title { color: red; font-weight: bold; }
    .dp-about .para { margin: 0 0 4px 0; }
    .dp-about .footer { background-color: #ECEADB; border-top: 1px solid #fff; text-align: right; }
    .dp-about .close { font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; background-color: #ECEADB; width: 60px; height: 22px; }
    /* Language specific styles */
    .dp-highlighter .comment, .dp-highlighter .comments { color: #008200; }
    .dp-highlighter .string { color: blue; }
    .dp-highlighter .keyword { color: #069; font-weight: bold; }
    .dp-highlighter .preprocessor { color: gray; }
  3. 儲存即可

使用方法

  • 方法一 - 使用 “<textarea>” 區塊,需將 “&” 轉換為 “&amp;”:
    <textarea name="code" class="程式語言">
    程式碼
    </textarea>
  • 方法二 - 使用 “<pre>” 區塊,需將 “&” 轉換為 “&amp;”、”<” 轉換為 “&lt;”、”>” 轉換為 “&gt;”:
    <pre name="code" class="程式語言">
    程式碼
    </pre>

參考網頁

在 Blogger 中使用 google-code-prettify 顯示程式碼

google-code-prettify 是一支不錯的程式碼高亮度顯示程式,特點為:

  1. 支援 15 種程式語言(但某些語言似乎還沒完成)
  2. 程式僅兩個檔案
  3. 載入速度較快

缺點為:

  1. 跟 IE 的相容性比較不佳,在 IE 內複製程式碼時無換行符號
  2. 無行號顯示
  3. 僅支援 "<pre>" 區塊
  4. 程式碼過長時不會自動顯示捲軸 (可透過修改 CSS 解決)

安裝步驟

  1. 修改 blogger 樣板,搜尋 "</head>",在 "上方" 加入
    <link href='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css' type='text/css' rel='stylesheet' />
    <script type='text/javascript' src='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js'></script>

    (因為 "偷吃步" 的關係,程式直接連到 google-code-prettify 的 SVN 上,你可以視情況下載回來放到自己的伺服器)

  2. 繼續搜尋 "<body>",在標籤內的 onload 事件加入:
    prettyPrint();
  3. 儲存即可。

使用方法

      使用 "<pre>" 區塊,需將 "&" 轉換為 "&amp;"、"<" 轉換為 "&lt;"、">" 轉換為 "&gt;":
<pre class="prettyprint">
程式碼
</pre>

參考網頁

[Perl]Perl 的相關網頁資源

手冊

教學/手冊

Read more

return top