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