john's tech blog

hope is coming


  • 首页

  • 标签

  • 归档

scala命令行引入scala文件

发表于 2016-10-22 | 更新于 2019-05-07

scala命令行引入scala文件

scala REPL

The Scala Interpreter (often called a REPL for Read-Evaluate-Print Loop).

Scala解释器读到一个表达式,对它进行求值,将它打印出来,接着再继续读下一个表达式。这个过程被称做读取–求值–打印–循环,即:REPL。
从技术上讲,scala程序并不是一个解释器。实际发生的是,你输入的内容被快速地编译成字节码,然后这段字节码交由Java虚拟机执行。正因为如此,大多数scala程序员更倾向于将它称做“REPL”
scala入门之REPL

scala -i

1
scala -i extractors.scala

运行 scala -help 可以看到如下提示:

preload before starting the repl```
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

在启动repl之前预先加载file。

按照这种方式引入的话,如果碰到companion对象,那么会报如下警告:

warning: previously defined class FreeUser is not a companion to object FreeUser
.
Companions must be defined together; you may wish to use :paste mode for this.
defined object PreminumUser

希望你用:paste模式。

### :paste file

先进入REPL,然后运行如下命令:

```:paste file

hexo流程梳理之database

发表于 2016-10-17 | 更新于 2019-05-07

Hexo List命令

Hexo List命令实际调用的是plugins/console/list/index.js文件,核心代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var store = {
page: require('./page'),
post: require('./post'),
route: require('./route'),
tag: require('./tag')
};

//外部是调用hexo实例的call函数
//call函数内部从Console中获取命令函数,然后再用js原生的call函数通过hexo实例调用
function listConsole(args){
/* jshint validthis: true */
var type = args._.shift();//弹出第一个参数
var self = this;//hexo实例

// Display help message if user didn't input any arguments
//如果没有输入参数就调用help命令
//this指向hexo对象
//比如输入list tag 那么type就是tag
if (!type || !store.hasOwnProperty(type)){
//如果只输入hexo list 或者 输入store对象中没有的参数
//先调用hexo实例的call方法
//方法内部调用help的入口函数
return this.call('help', {_: ['list']});
}
//调用hexo实例的load方法
//加载数据库db.json
//然后调用store中存储的type命令
return this.load().then(function(){
return store[type].call(self, args);
});
}

load函数内部核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
//...省略

//调用load_database.js文件,传入hexo实例
return loadDatabase(this).then(function(){
return Promise.all([
self.source.process(),
self.theme.process()
]);
}).then(function(){
//return;
return self._generate({cache: true});
}).nodeify(callback);

传入this参数是为了在loadDatabase内部调用hexo的db变量,也就是调用warehouse模块的load函数,db是在hexo构造函数中初始化的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

//Database为warehouse模块中的database.js
this.database = new Database({
version: dbVersion,//初始化为1
path: pathFn.join(base, 'db.json')//传入文件名
});

//db.json
return fs.exists(path).then(function(exist){
if (!exist) return;

log.debug('Loading database.');
return db.load();//load函数之后再展开细讲
}).then(function(){
ctx._dbLoaded = true;
}, function(){
log.error('Database load failed. Deleting database.');
return fs.unlink(path);
});

Hexo warehouse模块

database.js构造函数

hexo在构造函数中实例化Database之后,就通过register_models.js注册了database的model,相当于数据库的模型,这样就为之后调用model做准备:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//传入options参数,如上面所说的version和path
function Database(options){
/**
* Database options.
*
* @property {Object} options
* @private
*/

//调用扩展函数
this.options = extend({
version: 0,
onUpgrade: function(){},
onDowngrade: function(){}
}, options);

/**
* Models.
*
* @property {Object} _models
* @private
*/

this._models = {};

/**
* Model constructor for this database instance.
*
* @property {Function} Model
* @param {String} name
* @param {Schema|Object} [schema]
* @constructor
* @private
*/

var _Model = this.Model = function(name, schema){
//内部调用Model.js
Model.call(this, name, schema);
};

util.inherits(_Model, Model);
_Model.prototype._database = this;
}

database.js model方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

//hexo构造函数:传入hexo对象
registerModels(this);

//registerModels内部代码:
//加载所有模型对象
var models = require('../models');

module.exports = function(ctx){
var db = ctx.database;//又一次引用database

var keys = Object.keys(models);
var key = '';

for (var i = 0, len = keys.length; i < len; i++){
key = keys[i];
db.model(key, models[key](ctx));//db.model方法
}
};

其中../models包含的模型如下:

1
2
3
4
5
6
7
8
9
10
exports.Asset = require('./asset');
exports.Cache = require('./cache');
exports.Category = require('./category');
exports.Data = require('./data');
exports.Page = require('./page');
exports.Post = require('./post');
exports.PostAsset = require('./post_asset');
exports.PostCategory = require('./post_category');
exports.PostTag = require('./post_tag');
exports.Tag = require('./tag');//tag模型
1
2
3
4
5
6
7
8
9
Database.prototype.model = function(name, schema){
//构造函数中初始化this._models
if (this._models[name]){
return this._models[name];
}
//调用this.Model构造函数
var model = this._models[name] = new this.Model(name, schema);
return model;
};
  1. 流程如下:
    • hexo实例化初始化Database,然后注册各个model
    • hexo list命令调用database的load方法加载

算法--二分法查找

发表于 2016-10-17 | 更新于 2019-05-07

二分法

数学领域术语
对于区间[a,b]上连续不断且f(a)·f(b)<0的函数y=f(x),通过不断地把函数f(x)的零点所在的区间一分为二,使区间的两个端点逐步逼近零点,进而得到零点近似值的方法叫二分法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public int binarySearch(int[] data,int aim){//以int数组为例,aim为需要查找的数
int start = 0;
int end = data.length-1;
int mid = (start+end)/2;//a
while(data[mid]!=aim && end>start){//如果data[mid]等于aim则死循环,所以排除
if(data[mid]>aim){
end = mid-1;
}else if(data[mid]<aim){
start = mid+1;
}
mid = (start+end)/2;//b,注意a,b
}
return (data[mid]!=aim)?-1:mid;//返回结果
}

hexo流程梳理之别名注册

发表于 2016-10-15 | 更新于 2019-05-24

hexo 命令注册

hexo构造函数中实例化extend.console,在hexo cli中先注册hexo help version命令

核心的注册逻辑:

1
2
3
4
5
var c = this.store[name.toLowerCase()] = fn;
c.options = options;
c.desc = desc;

this.alias = abbrev(Object.keys(this.store));

abbrev模块

abbrev是Isaac Z. Schlueter 创建的一个handy for command-line scripts, or other cases where you want to be able to accept shorthands.(专门为了处理命令行脚本,或者接受速记)

Isaac Z. Schlueter :npm inventor and CEO. Early contributor and former BDFL of Node.js. Author of many JavaScripts. Been making internets for a pretty long time.
(npm的创造者和CEO)

BDFL:Benevolent Dictator For Life (BDFL) is a title given to a small number of open-source software development leaders, typically project founders who retain the final say in disputes or arguments within the community.
是给少数开源软件开发领导者的标题,类似在社区里有说话权的项目创始人

比如以下几位都是nodejs的贡献者:
Ryan Dahl(跟PAAS Heroku 有关)
Isaac Schlueter (在Oakland CA 奥克兰(美国加利福尼亚州西部港市))
Bert Belder (为node提供windows支持的主要开发者)
TJ Holowaychuk (大名鼎鼎的TJ,express,jade的作者)
Tim Caswell (connect的作者)
Felix Geisendörfer ( works on projects node-formidable, node-mysql 在Berlin, Germany (德国柏林))
Mikeal Rogers (request的作者,在旧金山 San Francisco)
Alexis Sellier ( less.js, vows 在柏林)
Jeremy Ashkenas ( CoffeeScript, underscore, backbone, docco 的作者 在纽约 NYC)
Jed Schmidt ( fab.js )
Marak Squires (mostly known for pumping out dozens of node.js libraries per month)
Peteris Krumins ( browserling 在拉脱维亚)
James Halliday ( dnode, optimist and browserify的作者)

abbrev是在注册命令时为了存储别名时用的一个模块,比如输入hexo n的时候hexo会识别出是调用了new命令。就像它在readme.md中所说的Just like ruby’s Abbrev.就像ruby中的Abbrev模块。

Usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var store = {

'new' : function(){},
'deploy' : function(){},
'publish': function(){}
};

console.log(Object.keys(store));
//print array
//[ 'new', 'deploy', 'publish' ]

var alias = abbrev(Object.keys(store));

console.log(alias);

//print
console.log(alias);

{ d: 'deploy',
de: 'deploy',
dep: 'deploy',
depl: 'deploy',
deplo: 'deploy',
deploy: 'deploy',
n: 'new',
ne: 'new',
new: 'new',
p: 'publish',
pu: 'publish',
pub: 'publish',
publ: 'publish',
publi: 'publish',
publis: 'publish',
publish: 'publish' }

hexo 命令获取

通过存储在Console实例中的alias字典对象中获取实际的命令Key(alias字典对象就是通过调用abbrev生成的),然后去实际的store对象中获取命令。

核心的获取逻辑:

1
2
3
4
	Console.prototype.get = function(name){
name = name.toLowerCase();
return this.store[this.alias[name]];
};

abbrev源码分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function abbrev (list) {
//如果参数不等于1 或者参数list不是数组
//那么就调用Array的slice方法
//返回一个新的数组,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素。
if (arguments.length !== 1 || !Array.isArray(list)) {
//返回新的list数组
list = Array.prototype.slice.call(arguments, 0)
}
//放入args数组
for (var i = 0, l = list.length, args = [] ; i < l ; i ++) {
args[i] = typeof list[i] === "string" ? list[i] : String(list[i])
}

//排序,调用lexSort方法
// sort them lexicographically(按字典顺序), so that they're next to their nearest kin
args = args.sort(lexSort)

// walk through each, seeing how much it has in common with the next and previous
var abbrevs = {}
, prev = ""
for (var i = 0, l = args.length ; i < l ; i ++) {
var current = args[i]
, next = args[i + 1] || ""
, nextMatches = true
, prevMatches = true
if (current === next) continue
//循环结束j要么找到不等于前后的字符 要么就是整个当前字符串
for (var j = 0, cl = current.length ; j < cl ; j ++) {
var curChar = current.charAt(j)
//跟next和prev元素内的字符做比较,如果都不相等,那么就直接跳出这个元素的比较循环
nextMatches = nextMatches && curChar === next.charAt(j)
prevMatches = prevMatches && curChar === prev.charAt(j)
if (!nextMatches && !prevMatches) {
j ++
break
}
}
prev = current//把prev指向当前args元素
if (j === cl) {
abbrevs[current] = current
continue
}
//比如abc a ab abc
for (var a = current.substr(0, j) ; j <= cl ; j ++) {
abbrevs[a] = current
a += current.charAt(j)
}
}
return abbrevs
}

function lexSort (a, b) {
return a === b ? 0 : a > b ? 1 : -1
}

可以尝试写个C#的版本。。

wordpress删除googleapi

发表于 2016-10-12 | 更新于 2019-05-09

wordpress加载慢

wordpress默认启用twentysixteen皮肤,注释掉wp-content/theme/twentysixteen/functions.php中的代码:

wordpress很有趣,今年是2016年,那么默认皮肤就是2016…

1
2
3
4
5
6
7
function twentysixteen_scripts() {
// Add custom fonts, used in the main stylesheet.
//注释掉这段code即可
//wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), null );

// Add Genericons, used in the main stylesheet.
wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.4.1' );

1…363738…47

John

232 日志
43 标签
GitHub Twitter
欢迎关注我的公众号:沉迷Spring
© 2023 johnwonder
由 Hexo 强力驱动 v3.2.0
|
主题 – NexT.Pisces v7.1.1
|
0%