在coffeescript中使用带有循环的一行语句

时间:2015-10-27 20:30:16

标签: javascript coffeescript

我想知道是否可以更改以下代码:

for key of index_of
 if file.text.indexOf(key) > -1

从行到一行,如:

if file.text.indexOf(key) > -1 for key of index_of

我已经尝试了一些事情,但实际上没有人工作......

感谢。

1 个答案:

答案 0 :(得分:0)

在这种情况下,将namespace TabControlTest { using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows.Input; public class MainWindowViewModel :INotifyPropertyChanged { private TabItem _selectedItem; private TabItem _homePageTabItem; private ObservableCollection<TabItem> _tabItems; private IList<TabItem> _tabBackingList; public MainWindowViewModel() { _homePageTabItem = new TabItem { Header = "Tab 1", Content = "Tab 1 Content", IsSelected = true, IsChecked = true }; _tabBackingList = new List<TabItem> { _homePageTabItem }; TabItems = new ObservableCollection<TabItem>(_tabBackingList); AddCommand = new RelayCommand(AddTabs); } public ICommand AddCommand { get; private set; } public ObservableCollection<TabItem> TabItems { get { return _tabItems; } set { if (_tabItems != value) { _tabItems = value; OnPropertyChanged(); } } } public TabItem SelectedItem { get { return _selectedItem; } set { if (_selectedItem != value) { _selectedItem = value; OnPropertyChanged(); } } } private void AddTabs(object param) { _tabBackingList.Clear(); _tabBackingList.Add(_homePageTabItem); var item2 = new TabItem { Header = "Tab 2", Content = "Tab 2 Content", IsSelected = true, IsChecked = true }; var item3 = new TabItem { Header = "Tab 3", Content = "Tab 3 Content", IsSelected = false }; _tabBackingList.Add(item2); _tabBackingList.Add(item3); TabItems = new ObservableCollection<TabItem>(_tabBackingList); } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } } namespace TabControlTest { using System; using System.Windows.Input; public class RelayCommand : ICommand { private readonly Predicate<object> _canExecute; private readonly Action<object> _execute; public RelayCommand(Action<object> execute) { _execute = execute; } public bool CanExecute(object parameter) { if (_canExecute == null) return true; return _canExecute(parameter); } public void Execute(object parameter) { _execute(parameter); } public event EventHandler CanExecuteChanged; } } namespace TabControlTest { using System.ComponentModel; using System.Runtime.CompilerServices; public class TabItem : INotifyPropertyChanged { private bool _isSelected; private bool _isChecked; public string Header { get; set; } public string Content { get; set; } public bool IsSelected { get { return _isSelected; } set { if (_isSelected != value) { _isSelected = value; OnPropertyChanged(); } } } public bool IsChecked { get { return _isChecked; } set { if (_isChecked != value) { _isChecked = value; OnPropertyChanged(); } } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } } namespace TabControlTest { using System.Windows; /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = new MainWindowViewModel(); } } } <Window x:Class="TabControlTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:tabControlTest="clr-namespace:TabControlTest" xmlns:uxc="http://schemas.thermofisher.com/2013/UXLibrary/Controls" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <Button Content="Add" Margin="5" Grid.Row="0" Width="100" HorizontalAlignment="Left" Command="{Binding AddCommand}" ></Button> <TabControl Margin="5" Grid.Row="1" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding TabItems}" > <TabControl.ItemContainerStyle> <Style TargetType="TabItem"> <Setter Property="Header" Value="{Binding Header}"/> <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"></Setter> </Style> </TabControl.ItemContainerStyle> <TabControl.ContentTemplate> <DataTemplate DataType="{x:Type tabControlTest:TabItem}"> <Button Content="{Binding Content}"></Button> </DataTemplate> </TabControl.ContentTemplate> </TabControl> </Grid> </Window> 子句(仅在the documentation中的示例中实际记录)添加到循环中可能是最简单的事情:

when

这与说法相同:

do_something_with_key for key of index_of when file.text.indexOf(key) > -1

如果您需要for key of index_of if(file.text.indexOf(key) > -1) do_something_with_key ,那么您可以将条件逻辑移动到循环体中并添加一些括号来处理优先级:

else

但这将是一个难以理解的混乱,所以长形式会更好:

(if expr then ... else ...) for key of index_of