通过Katas在工作中发布的一些编码,我偶然发现了这个问题,我不知道如何解决.
使用Java 8 Streams,给定正整数列表,生成一个整数列表,其中整数前面有一个更大的值.
Run Code Online (Sandbox Code Playgroud)[10, 1, 15, 30, 2, 6]以上输入将产生:
Run Code Online (Sandbox Code Playgroud)[1, 15, 2]因为1在15之前,15在30之前,2在6之前.
public List<Integer> findSmallPrecedingValues(final List<Integer> values) {
List<Integer> result = new ArrayList<Integer>();
for (int i = 0; i < values.size(); i++) {
Integer next = (i + 1 < values.size() ? values.get(i + 1) : -1);
Integer current = values.get(i);
if (current < next) {
result.push(current);
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我无法弄清楚如何在lambda中访问下一个.
return values.stream().filter(v -> v < next).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
map并映射到a …我有一个从REST请求返回的对象集合.Angular会自动使用a填充每个元素$$hashKey.问题是,当我在没有的情况下搜索该数组中的对象时$$hashKey,它返回-1.这是有道理的.不幸的是,我不知道它的价值$$hashKey.
是否有更有效的方法来搜索AngularJS中从REST请求返回的对象集合中的对象而不剥离$$hashKey属性?
function arrayObjectIndexOf(arr, obj) {
var regex = /,?"\$\$hashKey":".*?",?/;
var search = JSON.stringify(obj).replace(regex, '');
console.log(search);
for ( var i = 0, k = arr.length; i < k; i++ ){
if (JSON.stringify(arr[i]).replace(regex, '') == search) {
return i;
}
};
return -1;
};
Run Code Online (Sandbox Code Playgroud) 我在这个主题上看过很多其他帖子,但没有一个可以解决我的问题(我正在试着让我的头发工作!).我正在尝试在VS2013(Windows 8.1)中创建一个简单的单页网站并在Firefox中查看该页面而我无法做到 - 我得到"无法启动IIS Express Web服务器"
然后我收到来自IIS Express的错误消息"正在使用指定的端口:进程IIS Express已经使用了端口8080(进程ID'6332')建议:1.尝试切换到8080及更高版本以外的端口比1024. 2)停止使用端口'8080'的应用程序
当我点击Open Log File链接时,我得到:
Failed to register URL "http://localhost:8080/" for site "WebSite1" application "/". Error description: Cannot create a file when that file already exists. (0x800700b7)
Failed to register URL "http://localhost:63997/" for site "WebApplication2" application "/". Error description: Cannot create a file when that file already exists. (0x800700b7)
Failed to register URL "http://localhost:64532/" for site "WebApplication3" application "/". Error description: Cannot create a file when that file already exists. (0x800700b7)
Registration …Run Code Online (Sandbox Code Playgroud) 从有关invoke方法的文档中,我读到:
在集合中的每个元素上调用methodName命名的方法,返回每个调用方法的结果数组
因此,我假设以下代码是同义词,但事实并非如此:
_.map(items, function(item) {
return _.omit(item, 'fieldName');
})
_.invoke(items, _.omit, 'fieldName');
Run Code Online (Sandbox Code Playgroud)
在这种情况下,该invoke方法生成一个字符串数组,而map方法返回fieldName从每个项目中删除的项目数组.
invoke方法实现与map函数相同的结果?invoke在这种特殊情况下返回字符串数组?var items = [{id:1, name:'foo'},
{id:2, name:'bar'},
{id:3, name:'baz'},
{id:4, name:'qux'}];
console.log(
_.invoke(items, _.omit, 'id')
);
console.log(
_.map(items, function(item) {
return _.omit(item, 'id');
})
);Run Code Online (Sandbox Code Playgroud)
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.3.1/lodash.min.js"></script>Run Code Online (Sandbox Code Playgroud)
如何编写Jasmine测试来测试debounce运算符的可观察量?我已经关注了这篇博文,了解了应该如何测试的原则,但它似乎没有用.
下面是我用来创建observable的工厂:
import Rx from "rx/dist/rx.all";
import DOMFactory from "../utils/dom-factory";
import usernameService from "./username.service";
function createUsernameComponent(config) {
const element = DOMFactory(config);
const username = Rx.Observable
.fromEvent(element.find('input'), 'input')
.pluck('target', 'value')
.startWith(config.value);
const isAvailable = username
.debounce(500)
.tap(() => console.info('I am never called!'))
.flatMapLatest(usernameService.isAvailable)
.startWith(false);
const usernameStream = Rx.Observable.combineLatest(username, isAvailable)
.map((results) => {
const [username, isAvailable] = results;
return isAvailable ? username : ''
})
.distinctUntilChanged();
return Object.freeze({
stream: usernameStream,
view: element
});
}
export default createUsernameComponent;
Run Code Online (Sandbox Code Playgroud)
请注意, …
我正在学习AngularJS.我想打印出一个对象列表,并迭代一个对象的内部对象的属性.这看起来像是使用嵌套循环的标准过程,但是,它看起来并不那么简单.
我的控制器设置如下.从本质上讲,它是一个随机车辆列表.
var vehicleApp = angular.module("vehicleApp", []);
vehicleApp.controller('VehicleController', function ($scope) {
$scope.vehicles = [{
id: 0,
name: "car",
parts: {
wheels: 4,
doors: 4
}
}, {
id: 1,
name: "plane",
parts: {
wings: 2,
doors: 2
}
}, {
id: 2,
name: "boat",
parts: {
doors: 1
}
}];
});
Run Code Online (Sandbox Code Playgroud)
我想输出这样的车辆:
car
- wheels (4)
- doors (2)
plane
- wings (2)
- doors (2)
boat
- doors (1)
Run Code Online (Sandbox Code Playgroud)
我使用的模板是这样设置的:
<div ng-app="vehicleApp" ng-controller="VehicleController">
<p ng-repeat="vehicle in vehicles">
{{ vehicle.name …Run Code Online (Sandbox Code Playgroud) 使用GET来自a 的请求时$resource,成功响应仅在Microsoft Internet Explorer 9中为空数组.
成功的场景:
GET请求会在开发环境和本地环境中返回一组数据.失败的情景:
调试步骤:
var AnswerSetBySubjectByForm = function($resource) {
return $resource('/rest/answerset/subject/:idSubject/form/:idForm',
{ idSubject : '@idSubject', idForm : '@idForm'},
{'get' : {method:'GET', isArray:true}}
);
};
Run Code Online (Sandbox Code Playgroud)
var AnswerSetController = function($scope, AnswerSetBySubjectByForm) {
... …Run Code Online (Sandbox Code Playgroud) 我有一个数字字段,专门说" 输入一个数字 ".最终用户总是会输入一个字符串.当用户使用Google Chrome点击"重置"按钮时,包含文字的数字字段将不会重置.
要求是:
type="number"属性,因为它允许键盘在移动设备上弹出.有一个jsFiddle演示版.
<div ng-app="app" ng-controller="ctrl">
<p>
Enter a string into the "number" field. Clicking any of the buttons
will not clear the field.
</p>
<p>
Enter a number into the "number" field. Clicking any of the buttons
will clear the field.
</p>
<input type="number" ng-model="someValue" placeholder="Enter a number"/>
<button type="button" ng-click="set('hello, world!')">Set to Hello, World!</button>
<button type="button" ng-click="set(undefined)">Set to undefined</button>
<button type="button" ng-click="set('')">Set …Run Code Online (Sandbox Code Playgroud) 我想搜索几个匹配的字符串.每个匹配最终链接到对象数组中的对象属性.找到匹配项后,该匹配项将替换为对象中的另一个属性.问题是代码将始终在第二次匹配时返回null.
这是我正在使用的测试用例.为了简化问题,我只需用数字5替换所有匹配项,但请注意最终代码将使用变量值替换匹配项.
下面是我用来测试和调试问题的代码.有趣的是,如果我改变var str = '5 + QUESTION_2',QUESTION_2则成功替换为5.本质上,问题归结为第二个匹配总是返回null,即使它可以匹配.
var re = /( |^)(QUESTION_1|QUESTION_2|QUESTION_3)( |$)/g;
var str = 'QUESTION_1 + QUESTION_2';
var rep = 5;
matches = re.exec(str);
var re2 = new RegExp("( |^)(" + matches[2] + ")( |$)", "g");
console.log(matches); // Returns a match on QUESTION_1
str = str.replace(re2, rep);
console.log(str); // Returns 5+ QUESTION_2
matches = re.exec(str);
console.log(matches); // Returns a match on NULL - doesn't find QUESTION_2
re2 = new RegExp("( …Run Code Online (Sandbox Code Playgroud) 我想使用SailsJS" Find Where "蓝图路径创建一个包含多个条件的复杂查询.但是,我无法成功使用equals比较器和条件.我找不到关于如何实现Find Where路由的充分文档,因此我完成了源代码并提出了以下方案.
使用SailsJS Find Where Blueprint Route,如何实现:
以下方案将返回相应的响应:
http://localhost:1337/api/user?name=fred
http://localhost:1337/api/user?where={"name":{"startsWith":"fred"}}
http://localhost:1337/api/user?where={"name":{"endsWith":"fred"}}
http://localhost:1337/api/user?where={"name":{"contains":"fred"}}
http://localhost:1337/api/user?where={"name":{"like":"fred"}}
http://localhost:1337/api/user?where={"or":[{"name":{"startsWith":"fred"}}]}
http://localhost:1337/api/user?where={"or":[{"name":{"startsWith":"fred"}},{"path":{"endsWith":"fred"}}]}
Run Code Online (Sandbox Code Playgroud)
以下方案返回空响应:
http://localhost:1337/api/user?where={"name":{"equals":"fred"}}
http://localhost:1337/api/user?where={"name":{"=":"fred"}}
http://localhost:1337/api/user?where={"name":{"equal":"fred"}}
http://localhost:1337/api/user?where={"and":[{"name":{"startsWith":"fred"}}]}
http://localhost:1337/api/user?where={"and":[{"name":{"startsWith":"fred"}},{"path":{"endsWith":"fred"}}]}
Run Code Online (Sandbox Code Playgroud) 用例是我有一个zip包含许多csv文件的文件。然后将每个文件中的每一行发送到seda队列进行处理。我遇到的问题是我想知道何时每行都由seda队列以执行其他工作。我不确定该如何处理。当前,我正在调查使用轮询来测试seda队列何时为空,但是如果处理的行比到达的行快,这可能会产生错误的结果。
我有一个可解压缩zip文件并以读取每个文件的类InputStream。然后,将文件中的每一行发送到生产者,然后再将其发送到seda队列。
@Component
public class CsvProcessor {
@Resource(name = "csvLineProducer")
ProducerTemplate producer;
public void process(InputStream flatFileStream) throws IOException {
if (flatFileStream==null) return;
try {
LineIterator it = IOUtils.lineIterator(flatFileStream, "UTF-8");
while (it.hasNext()) {
final String recordLine = it.nextLine();
this.producer.send(new Processor() {
public void process(Exchange outExchange) {
outExchange.getIn().setBody(recordLine);
}
});
}
} finally {
IOUtils.closeQuietly(flatFileStream);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是骆驼的配置。
<camelContext xmlns="http://camel.apache.org/schema/spring" trace="true">
<template id="csvLineProducer" defaultEndpoint="seda:flatRecordStream"/> …Run Code Online (Sandbox Code Playgroud) 我目前在许多适配器中都有这种模式:
entries.stream()
.filter(Entry.class::isInstance)
.map(Entry.class::cast)
.map(Entry::getFooBar)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
条目是List实现特定接口的对象.不幸的是,界面 - 它是第三方库的一部分 - 没有定义常见的getter.要创建我想要的对象列表,我需要搜索它们,投射它们,并调用适当的getter方法.
我打算将它重构为一个帮助类:
public static <T, O> List<O> entriesToBeans(List<T> entries,
Class<T> entryClass, Supplier<O> supplier) {
return entries.stream()
.filter(entryClass::isInstance)
.map(entryClass::cast)
.map(supplier) // <- This line is invalid
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
然后我会调用此方法进行转换:
Helper.entriesToBeans(entries,
Entry_7Bean.class,
Entry_7Bean::getFooBar);
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法将getter传递给重构函数并让地图调用它,因为map它期待一个函数.
javascript ×5
angularjs ×4
java ×3
java-8 ×2
java-stream ×2
ajax ×1
apache-camel ×1
arrays ×1
html5 ×1
iis-express ×1
jasmine ×1
lodash ×1
mongoose ×1
node.js ×1
regex ×1
rxjs ×1
sails.js ×1
waterline ×1